package com.yuncheng.spcyApi.spcyService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.spcyApi.vo.fg.FgWtVo;
import com.yuncheng.spcyApi.vo.fg.FgWttkVo;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.bo.SqsxXcjcQxParamBo;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SpcyFgZjmlMapper;
import com.yuncheng.spcyApi.mapper.YcSysUserMapper;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import com.yuncheng.spcyApi.vo.common.Result;
import com.yuncheng.spcyApi.vo.fg.FgZjListVo;
import com.yuncheng.spcyApi.vo.sqsx.FlfgZjmlTreeVo;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.sqsx.HandXcjcwtTkVo;
import com.yuncheng.spcyApi.vo.sqsx.XcjcMsWtTkVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

@Component(value = "spcyFlfgService")
public class SpcyFlfgService {

    private static final Logger log = LoggerFactory.getLogger(SpcyFlfgService.class);

    @Resource
    @Lazy
    private ISpcyFgZdwjService spcyFgZdwjService;

    @Resource
    @Lazy
    private ISpcyFgFgmcService spcyFgFgmcService;

    @Resource
    @Lazy
    private ISpcyFgZjmlService spcyFgZjmlService;

    @Resource
    private SpcyFgZjmlMapper spcyFgZjmlMapper;

    @Resource
    private YcSysUserMapper ycSysUserMapper;

    @Resource
    @Lazy
    private ISpcyFgTkService spcyFgTkService;

    @Resource
    @Lazy
    private ISpcyFgZdyzService spcyFgZdyzService;

    @Resource
    @Lazy
    private ISpcyFgZjmsmbService spcyFgZjmsmbService;

    @Resource
    @Lazy
    private ISpcyRsJcxmService spcyRsJcxmService;

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;

    @Resource
    @Lazy
    private ISpcyFgXcjczjService spcyFgXcjczjService;

    @Resource
    @Lazy
    private ISpcyFgXcjcwtService spcyFgXcjcwtService;

    @Resource
    @Lazy
    private ISpcyFgXcjcwttkService spcyFgXcjcwttkService;

    @Resource
    @Lazy
    private ISpcyRsYwzdxxService spcyRsYwzdxxService;

    @Resource
    @Lazy
    private ISpcySqsxYwlxmbService spcySqsxYwlxmbService;

    @Resource
    @Lazy
    private ISpcySqsxBaiscService spcySqsxBaiscService;

    @Resource
    private SpcyTemplateService spcyTemplateService;

    // 是否启动索引条款
    private final boolean isEnableIndexTk = false;
//    private final boolean isEnableIndexXcjcwt = false;

    /** 初始化 - 药品生产 - 检查项目 生成 对应的法律法规库 */
    public void initFlfgInYpsc(String fzdwjid){
        if (StringUtils.isBlank(fzdwjid)){
            fzdwjid = GetUuIdUtils.ReplaceUuId();
        }

        String fwjqc = "《药品生产现场检查指导手册》";
        SpcyFgZdwj newZdwj = new SpcyFgZdwj();
        newZdwj.setId(fzdwjid);
        newZdwj.setFwjqc(fwjqc);
        newZdwj.setFwjjc(fwjqc);
        newZdwj.setForder(this.getZdwjNumber()+1);
        newZdwj.setFbb("("+String.valueOf(newZdwj.getForder())+")版,出版日期："+ DateConversionUtils.DateToYMDHMSChineseString(new Date()));
        spcyFgZdwjService.save(newZdwj);

        List<SpcyFgFgmc> newfgmcList = new ArrayList<>();
        List<SpcyFgZjml> newzjmlList = new ArrayList<>();
        List<SpcyFgTk> newtkList = new ArrayList<>();

        List<SpcyRsJcxm> ypscJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                .eq(SpcyRsJcxm::getFywflcode, "001002")
                .orderByAsc(SpcyRsJcxm::getFxh));

        // 法规名称
        List<String> fgmcJcxmList = ypscJcxmList.stream().map(SpcyRsJcxm::getFtype3).distinct().collect(Collectors.toList());

        for (int i=0; i<fgmcJcxmList.size(); i++){
            List<SpcyRsJcxm> zjmlJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                    .eq(SpcyRsJcxm::getFywflcode, "001002")
                    .eq(SpcyRsJcxm::getFtype3, fgmcJcxmList.get(i))
                    .orderByAsc(SpcyRsJcxm::getFxh));

            /**新法规名称*/
            SpcyFgFgmc newFgmc = new SpcyFgFgmc();
            newFgmc.setId(GetUuIdUtils.ReplaceUuId());
            newFgmc.setFzdwjid(fzdwjid);
            newFgmc.setForder(i+1);
            newFgmc.setFfgmc(fgmcJcxmList.get(i));
            newFgmc.setFfgjc(fgmcJcxmList.get(i));
            newFgmc.setFbb("1.0");

            newfgmcList.add(newFgmc);

            // 章节目录
            List<String> zjmlList = zjmlJcxmList.stream().map(SpcyRsJcxm::getFtype4).distinct().collect(Collectors.toList());

            for (int j=0; j<zjmlList.size(); j++) {

                /** 法规名称-新法规目录 */
                SpcyFgZjml newZjml = new SpcyFgZjml();
                newZjml.setId(GetUuIdUtils.ReplaceUuId());
                newZjml.setFfgid(newFgmc.getId()); // 法规id
                newZjml.setForder(j+1);
                newZjml.setFzjmc(zjmlList.get(j));
                newZjml.setFzjjc(zjmlList.get(j));

                newzjmlList.add(newZjml);

                List<SpcyRsJcxm> tkJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                        .eq(SpcyRsJcxm::getFywflcode, "001002")
                        .eq(SpcyRsJcxm::getFtype3, fgmcJcxmList.get(i))
                        .eq(SpcyRsJcxm::getFtype4, zjmlList.get(j))
                        .orderByAsc(SpcyRsJcxm::getFxh));

                /** 法规名称-新法规目录-条款 */
                for (int n=0; n<tkJcxmList.size(); n++){
                    SpcyFgTk newTk = new SpcyFgTk();
                    newTk.setId(GetUuIdUtils.ReplaceUuId());
                    newTk.setFfgid(newFgmc.getId());  // 法规id
                    newTk.setFzjid(newZjml.getId());  // 章节id
                    newTk.setForder(n+1);
                    newTk.setFzdyzh(tkJcxmList.get(n).getFtype5());
                    newTk.setFzdyznr(tkJcxmList.get(n).getFcontent1());

                    newtkList.add(newTk);
                }

            }
        }

        spcyFgFgmcService.saveBatch(newfgmcList);
        spcyFgZjmlService.saveBatch(newzjmlList);
        spcyFgTkService.saveBatch(newtkList);

        // 添加索引条款
//        fgTkIndexService.inintTk(newtkList);
    }

    /** 初始化 - 药品生产 - 检查项目 生成 对应的法律法规库 */
    public void initFlfgInYpscNew(String fzdwjid, String fywflcode){
        if (StringUtils.isBlank(fzdwjid)){
            fzdwjid = GetUuIdUtils.ReplaceUuId();
        }

        String fwjqc = "《药品生产现场检查指导手册》";
        SpcyFgZdwj newZdwj = new SpcyFgZdwj();
        newZdwj.setId(fzdwjid);
        newZdwj.setFwjqc(fwjqc);
        newZdwj.setFwjjc(fwjqc);
        newZdwj.setForder(this.getZdwjNumber()+1);
        newZdwj.setFbb("("+String.valueOf(newZdwj.getForder())+")版,出版日期："+ DateConversionUtils.DateToYMDHMSChineseString(new Date()));
        spcyFgZdwjService.save(newZdwj);

        List<SpcyFgFgmc> newfgmcList = new ArrayList<>();
        List<SpcyFgZjml> newzjmlList = new ArrayList<>();
        List<SpcyFgTk> newtkList = new ArrayList<>();

        List<SpcyRsJcxm> ypscJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                .eq(SpcyRsJcxm::getFywflcode, fywflcode)
                .orderByAsc(SpcyRsJcxm::getFxh));

        // 法规名称
        List<String> fgmcJcxmList = ypscJcxmList.stream().map(SpcyRsJcxm::getFtype3).distinct().collect(Collectors.toList());

        for (int i=0; i<fgmcJcxmList.size(); i++){
            List<SpcyRsJcxm> zjmlJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                    .eq(SpcyRsJcxm::getFywflcode, fywflcode)
                    .eq(SpcyRsJcxm::getFtype3, fgmcJcxmList.get(i))
                    .orderByAsc(SpcyRsJcxm::getFxh));

            /**新法规名称*/
            SpcyFgFgmc newFgmc = new SpcyFgFgmc();
            newFgmc.setId(GetUuIdUtils.ReplaceUuId());
            newFgmc.setFzdwjid(fzdwjid);
            newFgmc.setForder(i+1);
            newFgmc.setFfgmc(fgmcJcxmList.get(i));
            newFgmc.setFfgjc(fgmcJcxmList.get(i));
            newFgmc.setFbb("1.0");

            newfgmcList.add(newFgmc);

            // 章节目录
            List<String> zjmlList = zjmlJcxmList.stream().map(SpcyRsJcxm::getFtype4).distinct().collect(Collectors.toList());

            for (int j=0; j<zjmlList.size(); j++) {

                /** 法规名称-新法规目录 */
                SpcyFgZjml newZjml = new SpcyFgZjml();
                newZjml.setId(GetUuIdUtils.ReplaceUuId());
                newZjml.setFfgid(newFgmc.getId()); // 法规id
                newZjml.setForder(j+1);
                newZjml.setFzjmc(zjmlList.get(j));
                newZjml.setFzjjc(zjmlList.get(j));

                newzjmlList.add(newZjml);

                List<SpcyRsJcxm> tkJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                        .eq(SpcyRsJcxm::getFywflcode, fywflcode)
                        .eq(SpcyRsJcxm::getFtype3, fgmcJcxmList.get(i))
                        .eq(SpcyRsJcxm::getFtype4, zjmlList.get(j))
                        .orderByAsc(SpcyRsJcxm::getFxh));

                /** 法规名称-新法规目录-条款 */
                for (int n=0; n<tkJcxmList.size(); n++){
                    SpcyFgTk newTk = new SpcyFgTk();
                    newTk.setId(GetUuIdUtils.ReplaceUuId());
                    newTk.setFfgid(newFgmc.getId());  // 法规id
                    newTk.setFzjid(newZjml.getId());  // 章节id
                    newTk.setForder(n+1);
                    newTk.setFzdyzh(tkJcxmList.get(n).getFtype5());
                    newTk.setFzdyznr(tkJcxmList.get(n).getFcontent1());
                    newTk.setFjcyd(tkJcxmList.get(n).getFcontent2());

                    newtkList.add(newTk);
                }

            }
        }

        spcyFgFgmcService.saveBatch(newfgmcList);
        spcyFgZjmlService.saveBatch(newzjmlList);
        spcyFgTkService.saveBatch(newtkList);

        // 添加索引条款
//        fgTkIndexService.inintTk(newtkList);
    }

    /** 初始化 - 医疗器械 - 检查项目 生成 对应的法律法规库 */
    public void initFlfgInYlqx(String fzdwjid){
        if (StringUtils.isBlank(fzdwjid)){
            fzdwjid = GetUuIdUtils.ReplaceUuId();
        }
        String fwjqc = "《医疗器械查验手册》";

        SpcyFgZdwj newZdwj = new SpcyFgZdwj();
        newZdwj.setId(fzdwjid);
        newZdwj.setFwjqc(fwjqc);
        newZdwj.setFwjjc(fwjqc);
        newZdwj.setForder(this.getZdwjNumber()+1);
        newZdwj.setFbb("("+String.valueOf(newZdwj.getForder())+")版,出版日期："+ DateConversionUtils.DateToYMDHMSChineseString(new Date()));
        spcyFgZdwjService.save(newZdwj);

        List<SpcyFgFgmc> newfgmcList = new ArrayList<>();
        List<SpcyFgZjml> newzjmlList = new ArrayList<>();
        List<SpcyFgTk> newtkList = new ArrayList<>();
        List<SpcyFgZdyz> newZdyzList = new ArrayList<>();

        List<SpcyRsJcxm> jcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                .eq(SpcyRsJcxm::getFywflcode, "002")
                .orderByAsc(SpcyRsJcxm::getFxh));

        // 法规名称
        List<String> fgmcJcxmList = jcxmList.stream().map(SpcyRsJcxm::getFtype3).distinct().collect(Collectors.toList());

        for (int i=0; i<fgmcJcxmList.size(); i++){
            List<SpcyRsJcxm> zjmlJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                    .eq(SpcyRsJcxm::getFywflcode, "002")
                    .eq(SpcyRsJcxm::getFtype3, fgmcJcxmList.get(i))
                    .orderByAsc(SpcyRsJcxm::getFxh));

            /**新法规名称*/
            SpcyFgFgmc newFgmc = new SpcyFgFgmc();
            newFgmc.setId(GetUuIdUtils.ReplaceUuId());
            newFgmc.setFzdwjid(fzdwjid);
            newFgmc.setForder(i+1);
            newFgmc.setFfgmc(fgmcJcxmList.get(i));
            newFgmc.setFfgjc(fgmcJcxmList.get(i));
            newFgmc.setFbb("1.0");

            newfgmcList.add(newFgmc);

            // 章节目录
            List<String> zjmlList = zjmlJcxmList.stream().map(SpcyRsJcxm::getFtype5).distinct().collect(Collectors.toList());

            for (int j=0; j<zjmlList.size(); j++) {

                /** 法规名称-新法规目录 */
                SpcyFgZjml newZjml = new SpcyFgZjml();
                newZjml.setId(GetUuIdUtils.ReplaceUuId());
                newZjml.setFfgid(newFgmc.getId()); // 法规id
                newZjml.setForder(j+1);
                newZjml.setFzjmc(zjmlList.get(j));
                newZjml.setFzjjc(zjmlList.get(j));

                newzjmlList.add(newZjml);

                List<SpcyRsJcxm> tkJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                        .eq(SpcyRsJcxm::getFywflcode, "002")
                        .eq(SpcyRsJcxm::getFtype3, fgmcJcxmList.get(i))
                        .eq(SpcyRsJcxm::getFtype5, zjmlList.get(j))
                        .orderByAsc(SpcyRsJcxm::getFxh));

//                List<String> zdyzJcxmStringList = tkJcxmList.stream().map(SpcyRsJcxm::getFtype6).distinct().collect(Collectors.toList());

                /** 法规名称-新法规目录-条款 */
                for (int n=0; n<tkJcxmList.size(); n++){
                    SpcyFgTk newTk = new SpcyFgTk();
                    newTk.setId(GetUuIdUtils.ReplaceUuId());
                    newTk.setFfgid(newFgmc.getId());  // 法规id
                    newTk.setFzjid(newZjml.getId());  // 章节id
                    newTk.setForder(n+1);
                    newTk.setFzdyzh(Optional.ofNullable(tkJcxmList.get(n).getFtype6().trim()).orElseGet(()->""));
                    newTk.setFzdyznr(tkJcxmList.get(n).getFcontent1());

                    newtkList.add(newTk);

                    List<SpcyRsJcxm> zdyzJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                            .eq(SpcyRsJcxm::getFywflcode, "002")
                            .eq(SpcyRsJcxm::getFtype3, fgmcJcxmList.get(i))
                            .eq(SpcyRsJcxm::getFtype5, zjmlList.get(j))
                            .eq(SpcyRsJcxm::getFtype6, tkJcxmList.get(n).getFtype6())
                            .orderByAsc(SpcyRsJcxm::getFxh));
                    for (int z=0; z<zdyzJcxmList.size(); z++){
                        SpcyFgZdyz newZdyz = new SpcyFgZdyz();
                        newZdyz.setForder(z+1);
                        newZdyz.setId(GetUuIdUtils.ReplaceUuId());
                        newZdyz.setFfgid(newFgmc.getId());
                        newZdyz.setFzjid(newZjml.getId());
                        newZdyz.setFtkid(newTk.getId());
                        newZdyz.setFtkh(zdyzJcxmList.get(z).getFtype7());
                        newZdyz.setFfltkh(zdyzJcxmList.get(z).getFtype8());
                        newZdyz.setFzdyznr(zdyzJcxmList.get(z).getFcontent2());
                        newZdyz.setFflnr(zdyzJcxmList.get(z).getFcontent3());

                        newZdyzList.add(newZdyz);
                    }

                }

            }
        }

        spcyFgFgmcService.saveBatch(newfgmcList);
        spcyFgZjmlService.saveBatch(newzjmlList);
        spcyFgTkService.saveBatch(newtkList);
        spcyFgZdyzService.saveBatch(newZdyzList);
    }

    /** 初始化 - 药品流通 - 检查项目 生成 对应的法律法规库 */
    public void initFlfgInYplt(String fzdwjid){
        if (StringUtils.isBlank(fzdwjid)){
            fzdwjid = GetUuIdUtils.ReplaceUuId();
        }
        String fwjqc = "《药品经营现场检查指导原则》";
        SpcyFgZdwj newZdwj = new SpcyFgZdwj();
        newZdwj.setId(fzdwjid);
        newZdwj.setFwjqc(fwjqc);
        newZdwj.setFwjjc(fwjqc);
        newZdwj.setForder(this.getZdwjNumber()+1);
        newZdwj.setFbb("("+String.valueOf(newZdwj.getForder())+")版,出版日期："+ DateConversionUtils.DateToYMDHMSChineseString(new Date()));
        spcyFgZdwjService.save(newZdwj);

        List<SpcyFgFgmc> newfgmcList = new ArrayList<>();
        List<SpcyFgZjml> newzjmlList = new ArrayList<>();
        List<SpcyFgTk> newtkList = new ArrayList<>();

        List<SpcyRsJcxm> ypscJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                .eq(SpcyRsJcxm::getFywflcode, "001003")
                .orderByAsc(SpcyRsJcxm::getFxh));

        // 法规名称
        List<String> fgmcJcxmList = ypscJcxmList.stream().map(SpcyRsJcxm::getFtype3).distinct().collect(Collectors.toList());

        for (int i=0; i<fgmcJcxmList.size(); i++){
            List<SpcyRsJcxm> zjmlJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                    .eq(SpcyRsJcxm::getFywflcode, "001003")
                    .eq(SpcyRsJcxm::getFtype3, fgmcJcxmList.get(i))
                    .orderByAsc(SpcyRsJcxm::getFxh));

            /**新法规名称*/
            SpcyFgFgmc newFgmc = new SpcyFgFgmc();
            newFgmc.setId(GetUuIdUtils.ReplaceUuId());
            newFgmc.setFzdwjid(fzdwjid);
            newFgmc.setForder(i+1);
            newFgmc.setFfgmc(fgmcJcxmList.get(i));
            newFgmc.setFfgjc(fgmcJcxmList.get(i));
            newFgmc.setFbb("1.0");

            newfgmcList.add(newFgmc);

            // 章节目录
            List<String> zjmlList = zjmlJcxmList.stream().map(SpcyRsJcxm::getFtype5).distinct().collect(Collectors.toList());

            for (int j=0; j<zjmlList.size(); j++) {

                /** 法规名称-新法规目录 */
                SpcyFgZjml newZjml = new SpcyFgZjml();
                newZjml.setId(GetUuIdUtils.ReplaceUuId());
                newZjml.setFfgid(newFgmc.getId()); // 法规id
                newZjml.setForder(j+1);
                newZjml.setFzjmc(zjmlList.get(j));
                newZjml.setFzjjc(zjmlList.get(j));

                newzjmlList.add(newZjml);

                List<SpcyRsJcxm> tkJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                        .eq(SpcyRsJcxm::getFywflcode, "001003")
                        .eq(SpcyRsJcxm::getFtype3, fgmcJcxmList.get(i))
                        .eq(SpcyRsJcxm::getFtype5, zjmlList.get(j))
                        .orderByAsc(SpcyRsJcxm::getFxh));

                /** 法规名称-新法规目录-条款 */
                for (int n=0; n<tkJcxmList.size(); n++){
                    SpcyFgTk newTk = new SpcyFgTk();
                    newTk.setId(GetUuIdUtils.ReplaceUuId());
                    newTk.setFfgid(newFgmc.getId());  // 法规id
                    newTk.setFzjid(newZjml.getId());  // 章节id
                    newTk.setForder(n+1);
                    newTk.setFzdyzh(tkJcxmList.get(n).getFtype7());
                    newTk.setFzdyznr(tkJcxmList.get(n).getFcontent1());

                    newtkList.add(newTk);
                }

            }
        }

        spcyFgFgmcService.saveBatch(newfgmcList);
        spcyFgZjmlService.saveBatch(newzjmlList);
        spcyFgTkService.saveBatch(newtkList);
    }

    /** 初始化 - 化妆品 - 检查项目 生成 对应的法律法规库 */
    public void initFlfgInHzp(String fzdwjid){
        if (StringUtils.isBlank(fzdwjid)){
            fzdwjid = GetUuIdUtils.ReplaceUuId();
        }

        String fwjqc = "《化妆品生产质量管理规范检查要点》";
        SpcyFgZdwj newZdwj = new SpcyFgZdwj();
        newZdwj.setId(fzdwjid);
        newZdwj.setFwjqc(fwjqc);
        newZdwj.setFwjjc(fwjqc);
        newZdwj.setForder(this.getZdwjNumber()+1);
        newZdwj.setFbb("("+String.valueOf(newZdwj.getForder())+")版,出版日期："+ DateConversionUtils.DateToYMDHMSChineseString(new Date()));
        spcyFgZdwjService.save(newZdwj);

        List<SpcyFgFgmc> newfgmcList = new ArrayList<>();
        List<SpcyFgZjml> newzjmlList = new ArrayList<>();
        List<SpcyFgTk> newtkList = new ArrayList<>();
        List<SpcyFgZdyz> newZdyzList = new ArrayList<>();

        List<SpcyRsJcxm> ypscJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                .eq(SpcyRsJcxm::getFywflcode, "003")
                .orderByAsc(SpcyRsJcxm::getFxh));

        // 法规名称
        List<String> fgmcJcxmList = ypscJcxmList.stream().map(SpcyRsJcxm::getFtype3).distinct().collect(Collectors.toList());

        for (int i=0; i<fgmcJcxmList.size(); i++){
            List<SpcyRsJcxm> zjmlJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                    .eq(SpcyRsJcxm::getFywflcode, "003")
                    .eq(SpcyRsJcxm::getFtype3, fgmcJcxmList.get(i))
                    .orderByAsc(SpcyRsJcxm::getFxh));

            /**新法规名称*/
            SpcyFgFgmc newFgmc = new SpcyFgFgmc();
            newFgmc.setId(GetUuIdUtils.ReplaceUuId());
            newFgmc.setFzdwjid(fzdwjid);
            newFgmc.setForder(i+1);
            newFgmc.setFfgmc(fgmcJcxmList.get(i));
            newFgmc.setFfgjc(fgmcJcxmList.get(i));
            newFgmc.setFbb("1.0");

            newfgmcList.add(newFgmc);

            // 章节目录
            List<String> zjmlList = zjmlJcxmList.stream().map(SpcyRsJcxm::getFtype4).distinct().collect(Collectors.toList());

            for (int j=0; j<zjmlList.size(); j++) {

                /** 法规名称-新法规目录 */
                SpcyFgZjml newZjml = new SpcyFgZjml();
                newZjml.setId(GetUuIdUtils.ReplaceUuId());
                newZjml.setFfgid(newFgmc.getId()); // 法规id
                newZjml.setForder(j+1);
                newZjml.setFzjmc(zjmlList.get(j));
                newZjml.setFzjjc(zjmlList.get(j));

                newzjmlList.add(newZjml);

                List<SpcyRsJcxm> tkJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                        .eq(SpcyRsJcxm::getFywflcode, "003")
                        .eq(SpcyRsJcxm::getFtype3, fgmcJcxmList.get(i))
                        .eq(SpcyRsJcxm::getFtype4, zjmlList.get(j))
                        .orderByAsc(SpcyRsJcxm::getFxh));

                /** 法规名称-新法规目录-条款 */
                for (int n=0; n<tkJcxmList.size(); n++){
                    SpcyFgTk newTk = new SpcyFgTk();
                    newTk.setId(GetUuIdUtils.ReplaceUuId());
                    newTk.setFfgid(newFgmc.getId());  // 法规id
                    newTk.setFzjid(newZjml.getId());  // 章节id
                    newTk.setForder(n+1);
                    newTk.setFzdyzh(tkJcxmList.get(n).getFtype5());
                    newTk.setFzdyznr(tkJcxmList.get(n).getFcontent1());

                    newtkList.add(newTk);

                    List<SpcyRsJcxm> zdyzJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                            .eq(SpcyRsJcxm::getFywflcode, "003")
                            .eq(SpcyRsJcxm::getFtype3, fgmcJcxmList.get(i))
                            .eq(SpcyRsJcxm::getFtype4, zjmlList.get(j))
                            .eq(SpcyRsJcxm::getFtype5, tkJcxmList.get(n).getFtype5())
                            .orderByAsc(SpcyRsJcxm::getFxh));
                    for (int z=0; z<zdyzJcxmList.size(); z++){
                        SpcyFgZdyz newZdyz = new SpcyFgZdyz();
                        newZdyz.setForder(z+1);
                        newZdyz.setId(GetUuIdUtils.ReplaceUuId());
                        newZdyz.setFfgid(newFgmc.getId());
                        newZdyz.setFzjid(newZjml.getId());
                        newZdyz.setFtkid(newTk.getId());
                        newZdyz.setFtkh(zdyzJcxmList.get(z).getFtype7());
                        newZdyz.setFzdyznr(zdyzJcxmList.get(z).getFcontent2());


                        newZdyzList.add(newZdyz);
                    }
                }

            }
        }

        spcyFgFgmcService.saveBatch(newfgmcList);
        spcyFgZjmlService.saveBatch(newzjmlList);
        spcyFgTkService.saveBatch(newtkList);
        spcyFgZdyzService.saveBatch(newZdyzList);
    }

    /** 初始化 - 药品注册 - 检查项目 生成 对应的法律法规库 */
    public void initFlfgInYpzc(String fzdwjid){
        if (StringUtils.isBlank(fzdwjid)){
            fzdwjid = GetUuIdUtils.ReplaceUuId();
        }
        String fwjqc = "《药品注册现场检查指导手册》";

        SpcyFgZdwj newZdwj = new SpcyFgZdwj();
        newZdwj.setId(fzdwjid);
        newZdwj.setFwjqc(fwjqc);
        newZdwj.setFwjjc(fwjqc);
        newZdwj.setForder(this.getZdwjNumber()+1);
        newZdwj.setFbb("("+String.valueOf(newZdwj.getForder())+")版,出版日期："+ DateConversionUtils.DateToYMDHMSChineseString(new Date()));
        spcyFgZdwjService.save(newZdwj);

        List<SpcyFgFgmc> newfgmcList = new ArrayList<>();
        List<SpcyFgZjml> newzjmlList = new ArrayList<>();
        List<SpcyFgTk> newtkList = new ArrayList<>();
        List<SpcyFgZdyz> newZdyzList = new ArrayList<>();

        List<SpcyRsJcxm> ypscJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                .eq(SpcyRsJcxm::getFywflcode, "001001")
                .orderByAsc(SpcyRsJcxm::getFxh));

        // 法规名称
        List<String> fgmcJcxmList = ypscJcxmList.stream().map(SpcyRsJcxm::getFtype3).distinct().collect(Collectors.toList());

        for (int i=0; i<fgmcJcxmList.size(); i++){
            List<SpcyRsJcxm> zjmlJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                    .eq(SpcyRsJcxm::getFywflcode, "001001")
                    .eq(SpcyRsJcxm::getFtype3, fgmcJcxmList.get(i))
                    .orderByAsc(SpcyRsJcxm::getFxh));

            /**新法规名称*/
            SpcyFgFgmc newFgmc = new SpcyFgFgmc();
            newFgmc.setId(GetUuIdUtils.ReplaceUuId());
            newFgmc.setFzdwjid(fzdwjid);
            newFgmc.setForder(i+1);
            newFgmc.setFfgmc(fgmcJcxmList.get(i));
            newFgmc.setFfgjc(fgmcJcxmList.get(i));
            newFgmc.setFbb("1.0");

            newfgmcList.add(newFgmc);

            // 章节目录
            List<String> zjmlList = zjmlJcxmList.stream().map(SpcyRsJcxm::getFtype4).distinct().collect(Collectors.toList());

            for (int j=0; j<zjmlList.size(); j++) {

                /** 法规名称-新法规目录 */
                SpcyFgZjml newZjml = new SpcyFgZjml();
                newZjml.setId(GetUuIdUtils.ReplaceUuId());
                newZjml.setFfgid(newFgmc.getId()); // 法规id
                newZjml.setForder(j+1);
                newZjml.setFzjmc(zjmlList.get(j));
                newZjml.setFzjjc(zjmlList.get(j));

                newzjmlList.add(newZjml);

                List<SpcyRsJcxm> tkJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                        .eq(SpcyRsJcxm::getFywflcode, "001001")
                        .eq(SpcyRsJcxm::getFtype3, fgmcJcxmList.get(i))
                        .eq(SpcyRsJcxm::getFtype4, zjmlList.get(j))
                        .orderByAsc(SpcyRsJcxm::getFxh));

                /** 法规名称-新法规目录-条款 */
                for (int n=0; n<tkJcxmList.size(); n++){
                    SpcyFgTk newTk = new SpcyFgTk();
                    newTk.setId(GetUuIdUtils.ReplaceUuId());
                    newTk.setFfgid(newFgmc.getId());  // 法规id
                    newTk.setFzjid(newZjml.getId());  // 章节id
                    newTk.setForder(n+1);
                    newTk.setFzdyzh(tkJcxmList.get(n).getFtype5());
                    newTk.setFzdyznr(tkJcxmList.get(n).getFcontent1());

                    newtkList.add(newTk);

                    List<SpcyRsJcxm> zdyzJcxmList = spcyRsJcxmService.list(new LambdaQueryWrapper<SpcyRsJcxm>()
                            .eq(SpcyRsJcxm::getFywflcode, "001001")
                            .eq(SpcyRsJcxm::getFtype3, fgmcJcxmList.get(i))
                            .eq(SpcyRsJcxm::getFtype4, zjmlList.get(j))
                            .eq(SpcyRsJcxm::getFtype5, tkJcxmList.get(n).getFtype5())
                            .orderByAsc(SpcyRsJcxm::getFxh));
                    for (int z=0; z<zdyzJcxmList.size(); z++){
                        SpcyFgZdyz newZdyz = new SpcyFgZdyz();
                        newZdyz.setForder(z+1);
                        newZdyz.setId(GetUuIdUtils.ReplaceUuId());
                        newZdyz.setFfgid(newFgmc.getId());
                        newZdyz.setFzjid(newZjml.getId());
                        newZdyz.setFtkid(newTk.getId());
                        newZdyz.setFtkh(zdyzJcxmList.get(z).getFtype7());
                        newZdyz.setFzdyznr(zdyzJcxmList.get(z).getFcontent2());


                        newZdyzList.add(newZdyz);
                    }
                }

            }
        }

        spcyFgFgmcService.saveBatch(newfgmcList);
        spcyFgZjmlService.saveBatch(newzjmlList);
        spcyFgTkService.saveBatch(newtkList);
        spcyFgZdyzService.saveBatch(newZdyzList);
    }

    /******************************************【 查询功能 】*****************************************************/

    /**
     * 指导文件-分页查询
     */
    public HttpResult queryPageZdwj(SpcyFgZdwj zdwj,Integer pageNo,Integer pageSize){
        Page<SpcyFgZdwj> page = new Page<>(pageNo,pageSize);

        LambdaQueryWrapper<SpcyFgZdwj> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(zdwj.getFbb()),SpcyFgZdwj::getFbb, zdwj.getFbb());
        queryWrapper.like(StringUtils.isNotBlank(zdwj.getFwjjc()),SpcyFgZdwj::getFwjjc, zdwj.getFwjjc());
        queryWrapper.like(StringUtils.isNotBlank(zdwj.getFwjqc()),SpcyFgZdwj::getFwjqc, zdwj.getFwjqc());
        queryWrapper.orderByAsc(SpcyFgZdwj::getForder);
        IPage<SpcyFgZdwj> pageList = spcyFgZdwjService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }

    /**
     * 根据指导文件id-获取对应法规列表
     * @param fzdwjid 指导文件id
     * @return
     */
    public HttpResult queryListFgByZdwjId(String fzdwjid){
        if (StringUtils.isBlank(fzdwjid)){
            return HttpResult.error("请提供指导文件参数！");
        }
        List<SpcyFgFgmc> fgList = spcyFgFgmcService.list(new LambdaQueryWrapper<SpcyFgFgmc>()
                .eq(SpcyFgFgmc::getFzdwjid, fzdwjid)
                .orderByAsc(SpcyFgFgmc::getForder));

        return HttpResult.ok(fgList);
    }

    /**
     * 根据法规id-获取章节（树形）列表
     * @param ffgid 法规id
     */
    public HttpResult queryListZjTreeByFgId(String ffgid){
        if (StringUtils.isBlank(ffgid)){
            return HttpResult.error("请提供法规参数！");
        }

        // 章节数组列表
//        List<SpcyFgZjml> zjList = this.queryZjListByFgId(ffgid);

        // 章节树形列表
        List<FlfgZjmlTreeVo> zjList = this.initZjTree(ffgid, null);

        return HttpResult.ok(zjList);
    }

    /**
     * 根据章节id-获取条款分页列表
     * @param tk (fzjid: 章节id【必传】)
     */
    public HttpResult queryPageTkListByZjId(SpcyFgTk tk,Integer pageNo,Integer pageSize){
        if (StringUtils.isBlank(tk.getFzjid())){
            return HttpResult.error("请选择章节！");
        }

        Page<SpcyFgTk> page = new Page<>(pageNo,pageSize);

        if (StringUtils.isBlank(tk.getFzdyznr())) {
            IPage<SpcyFgTk> pageList = spcyFgZjmlMapper.queryPageTkListByZjId(page, tk);

            return HttpResult.ok(pageList);
        }else {

            IPage<SpcyFgTk> pageList = spcyFgZjmlMapper.queryPageTkListByZjId(page, tk);

            return HttpResult.ok(pageList);
        }
    }

    /**
     * 根据章节获取条款
     * @param id 法规id or 章节id
     */
    public HttpResult queryTkList(String id, String gjz, PageVo pageVo){
        Page<SpcyFgTk> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());

        /** 判断是否为法规id */
        String ffgid = spcyFgZjmlMapper.selectFfgidById(id);
        String thk = null;
        if (StringUtils.isNotBlank(ffgid)){
            id = null;
        }else {
            ffgid = null;

            SpcyFgTk orgTkh = spcyFgTkService.getById(id);
            if (orgTkh != null){
                id = orgTkh.getFzjid();
                thk = orgTkh.getFzdyzh();
            }
        }
        IPage<SpcyFgTk> pageList = spcyFgZjmlMapper.selectPageXcjcTkListByXcjcZjId(page, id, ffgid, thk, gjz);
        return HttpResult.ok(pageList);
    }

    /**
     * 查询申请事项已选择的条款
     */
    public HttpResult queryYxtkList(String sqsxid){
        List<SpcyFgTk> pageList = spcyFgZjmlMapper.selectSqsxWttkList(sqsxid);
        return HttpResult.ok(pageList);
    }

    /**
     * 根据现场检查章节id-获取条款分页列表
     * @param fxcjczjid 现场检查章节id
     * @param gjz 关键字
     */
    public HttpResult queryPageXcjcTkListByXcjcZjId(String fxcjczjid,String gjz,Integer pageNo,Integer pageSize){
        if (StringUtils.isBlank(fxcjczjid)){
            return HttpResult.error("请选择对应章节！");
        }
        // 获取对应的章节id
        SpcyFgXcjczj zj = spcyFgXcjczjService.getById(fxcjczjid);
        if (zj == null){
            return HttpResult.error("当前检查章节数据不存在！");
        }
        fxcjczjid = zj.getFzjid();
        String fgid = zj.getFfgid(); // 法规id

        Page<SpcyFgTk> page = new Page<>(pageNo,pageSize);

        if (!isEnableIndexTk) {
            IPage<SpcyFgTk> pageList = spcyFgZjmlMapper.selectPageXcjcTkListByXcjcZjId(page, fxcjczjid, fgid, null, gjz);
            return HttpResult.ok(pageList);
        }else {
            if (StringUtils.isBlank(gjz)) {
                List<SpcyFgTk> pageList = spcyFgTkService.list(new LambdaQueryWrapper<SpcyFgTk>()
                        .eq(SpcyFgTk::getFzjid, fxcjczjid)
                        .orderByAsc(SpcyFgTk::getForder)
                        .orderByAsc(SpcyFgTk::getCreateTime));

                Map result = new HashMap();
                result.put("records", pageList);
                result.put("current", 1);
                result.put("size", pageList.size());
                result.put("total", pageList.size());
                result.put("pages", 1);
                return HttpResult.ok(result);
            } else {

                return HttpResult.ok();
            }
        }
    }

    /**
     * 查询现场检查章节-发现问题历史库
     * @param fxcjczjid 现场检查章节id
     * @param gjz 关键字
     */
    public HttpResult queryPageXcjcHistoryTkListByXcjcZjId(String fxcjczjid,String gjz,Integer pageNo,Integer pageSize){
        if (StringUtils.isBlank(fxcjczjid)){
            return HttpResult.error("检查章节参数为空！");
        }

        SpcyFgXcjczj zj = spcyFgXcjczjService.getById(fxcjczjid);
        if (zj == null){
            return HttpResult.error("当前检查章节不存在！");
        }
        fxcjczjid = zj.getFzjid();

        String fgid = zj.getFfgid();


        Page<Map<String,Object>> page = new Page<>(pageNo,pageSize);
        IPage<Map<String,Object>> pageList = spcyFgZjmlMapper.selectPageXcjcHistoryZjWtListByXcjcZjId(page, fxcjczjid, fgid, gjz);

        List<Map<String,Object>> oldrecordsList = pageList.getRecords();

        List<Map<String,Object>> newRecordsMapList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(oldrecordsList)) {
            for (Map<String, Object> map : oldrecordsList) {
                try {
                    map = MapUtils.keyToLowerCaseAndString(map);
                } catch (IOException e) {
                    log.error("查询现场检查章节-发现问题历史库：[IOException]->{}",e.getMessage());
                } catch (SQLException e) {
                    log.error("查询现场检查章节-发现问题历史库：[SQLException]->{}",e.getMessage());
                }
                newRecordsMapList.add(map);
            }
        }
        pageList.setRecords(newRecordsMapList);

        return HttpResult.ok(pageList);
    }

    /**
     * 根据章节id-获取指导原则分页列表 (废弃)
     * @param zdyz (fzjid: 章节id【必传】)
     */
    public HttpResult queryPageZdyzListByZjId(SpcyFgZdyz zdyz,Integer pageNo,Integer pageSize){
        if (StringUtils.isBlank(zdyz.getFzjid())){
            return HttpResult.error("请选择章节！");
        }

        Page<SpcyFgZdyz> page = new Page<>(pageNo,pageSize);

        IPage<SpcyFgZdyz> pageList = spcyFgZjmlMapper.queryPageZdyzListByZjId(page, zdyz);
        /*LambdaQueryWrapper<SpcyFgZdyz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(zdyz.getFzjid()),SpcyFgZdyz::getFzjid, zdyz.getFzjid());
        queryWrapper.like(StringUtils.isNotBlank(zdyz.getFtkh()),SpcyFgZdyz::getFtkh, zdyz.getFtkh());
        queryWrapper.like(StringUtils.isNotBlank(zdyz.getFzdyznr()),SpcyFgZdyz::getFzdyznr, zdyz.getFzdyznr());

        queryWrapper.orderByDesc(SpcyFgZdyz::getForder);
        IPage<SpcyFgZdyz> pageList = spcyFgZdyzService.page(page, queryWrapper);*/

        return HttpResult.ok(pageList);
    }

    /**
     * 根据条款id-获取指导原则分页列表
     */
    public HttpResult queryPageZdyzListByTkId(SpcyFgZdyz zdyz,Integer pageNo,Integer pageSize){
        if (StringUtils.isBlank(zdyz.getFtkid())){
            return HttpResult.error("请选择条款！");
        }
        Page<SpcyFgZdyz> page = new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<SpcyFgZdyz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(zdyz.getFtkid()),SpcyFgZdyz::getFtkid, zdyz.getFtkid());
        queryWrapper.like(StringUtils.isNotBlank(zdyz.getFtkid()),SpcyFgZdyz::getFtkid, zdyz.getFtkid());
        queryWrapper.like(StringUtils.isNotBlank(zdyz.getFtkh()),SpcyFgZdyz::getFtkh, zdyz.getFtkh());
        queryWrapper.like(StringUtils.isNotBlank(zdyz.getFzdyznr()),SpcyFgZdyz::getFzdyznr, zdyz.getFzdyznr());
        queryWrapper.like(StringUtils.isNotBlank(zdyz.getFflnr()),SpcyFgZdyz::getFflnr, zdyz.getFflnr());
        queryWrapper.like(StringUtils.isNotBlank(zdyz.getFfltkh()),SpcyFgZdyz::getFfltkh, zdyz.getFfltkh());

        queryWrapper.orderByDesc(SpcyFgZdyz::getForder);
        IPage<SpcyFgZdyz> pageList = spcyFgZdyzService.page(page, queryWrapper);
        return HttpResult.ok(pageList);
    }

    /**
     * 根据章节id-获取模版分页列表
     * @param mb (fzjid: 章节id【必传】)
     */
    public HttpResult queryPageMbListByZjId(SpcyFgZjmsmb mb,Integer pageNo,Integer pageSize){
        if (StringUtils.isBlank(mb.getFzjid())){
            return HttpResult.error("请选择章节！");
        }

        Page<SpcyFgZjmsmb> page = new Page<>(pageNo,pageSize);

        IPage<SpcyFgZjmsmb> pageList = spcyFgZjmlMapper.queryPageMbListByZjId(page, mb);
        /*LambdaQueryWrapper<SpcyFgZjmsmb> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(mb.getFzjid()),SpcyFgZjmsmb::getFzjid, mb.getFzjid());
        queryWrapper.like(StringUtils.isNotBlank(mb.getFly()),SpcyFgZjmsmb::getFly, mb.getFly());
        queryWrapper.like(StringUtils.isNotBlank(mb.getFzjmc()),SpcyFgZjmsmb::getFzjmc, mb.getFzjmc());

        queryWrapper.orderByDesc(SpcyFgZjmsmb::getCreateTime);
        IPage<SpcyFgZjmsmb> pageList = spcyFgZjmsmbService.page(page, queryWrapper);*/

        return HttpResult.ok(pageList);
    }

    /**
     * 指导文件-查看界面
     * @param zdwjid 指导文件id
     */
    public Map viewZdwjByZdwjId(String zdwjid){
        Map resultMap = new HashMap();

        // 指导手册文件内容
        SpcyFgZdwj zdwj = spcyFgZdwjService.getById(zdwjid);
        resultMap.put("zdwj", zdwj);

        // 法规内容
        List<Map> fgMapList = new ArrayList<>();

        // 章节目录-树形列表
        List<Map> treeMapList = new ArrayList<>();
        Map treeMap = null;

        List<SpcyFgFgmc> fgList = this.queryFgListByZdwjId(zdwjid);
        for (int i=0; i<fgList.size(); i++){

            /** 章节目录-树形列表 */
            treeMap = new HashMap();
            treeMap.put("fgid", fgList.get(i).getId());
            treeMap.put("fgjc", fgList.get(i).getFfgjc());
//            List<SpcyFgZjml> zjList = this.queryZjListByFgId(fgList.get(i).getId());
            List<FlfgZjmlTreeVo> zjList = this.initZjTree(fgList.get(i).getId(), null);
            treeMap.put("zjList", zjList);

            treeMapList.add(treeMap);

            /** 法规内容 */
            List<Map> tkMapList = new ArrayList<>();
            Map fgMap = new HashMap();
            fgMap.put("fgid", fgList.get(i).getId());
            fgMap.put("fgmc", fgList.get(i).getFfgmc());

            /** 条款内容 */
            Map tkMap = null;
            for (int j=0; j<zjList.size(); j++){
                tkMap = new HashMap();
                tkMap.put("id", zjList.get(j).getId());
                tkMap.put("zjmc", zjList.get(j).getFzjmc());
                List<SpcyFgTk> tkList = this.queryTkListByZjId(zjList.get(j).getId());
                tkMap.put("tkList", tkList);

                tkMapList.add(tkMap);
            }
            fgMap.put("tk", tkMapList);

            fgMapList.add(fgMap);
        }

        resultMap.put("treeZj", treeMapList);
        resultMap.put("fg", fgMapList);

        return resultMap;
    }

    /**
     * 指导文件-查看界面
     * @param zdwjid 指导文件id
     */
    public Map viewZdwjByZdwjIdAndGjz(String zdwjid, String gjz){
        Map resultMap = new HashMap();

        // 指导手册文件内容
        SpcyFgZdwj zdwj = spcyFgZdwjService.getById(zdwjid);
        resultMap.put("zdwj", zdwj);

        // 法规内容
        List<Map> fgMapList = new ArrayList<>();

        // 章节目录-树形列表
        List<Map> treeMapList = new ArrayList<>();
        Map treeMap = null;

        List<SpcyFgFgmc> fgList = this.queryFgListByZdwjId(zdwjid);
        for (int i=0; i<fgList.size(); i++){

            /** 章节目录-树形列表 */
            treeMap = new HashMap();
            treeMap.put("fgid", fgList.get(i).getId());
            treeMap.put("fgjc", fgList.get(i).getFfgjc());

            List<FlfgZjmlTreeVo> zjList = this.initZjTree(fgList.get(i).getId(), null, gjz);
            treeMap.put("zjList", zjList);

            treeMapList.add(treeMap);

            /** 法规内容 */
            List<Map> tkMapList = new ArrayList<>();
            Map fgMap = new HashMap();
            fgMap.put("fgid", fgList.get(i).getId());
            fgMap.put("fgmc", fgList.get(i).getFfgmc());

            /** 条款内容 */
            Map tkMap = null;
            for (int j=0; j<zjList.size(); j++){
                tkMap = new HashMap();
                tkMap.put("id", zjList.get(j).getId());
                tkMap.put("zjmc", zjList.get(j).getFzjmc());
                List<SpcyFgTk> tkList = this.queryTkListByZjId(zjList.get(j).getId());
                tkMap.put("tkList", tkList);

                tkMapList.add(tkMap);
            }
            fgMap.put("tk", tkMapList);

            fgMapList.add(fgMap);
        }

        resultMap.put("treeZj", treeMapList);
        resultMap.put("fg", fgMapList);

        return resultMap;
    }

    /******************************************【 指导文件 】*****************************************************/

    /**
     * 获取指导文件存在数量
     */
    private int getZdwjNumber(){
        List<SpcyFgZdwj> countList = spcyFgZdwjService.list();

        return countList.size();
    }

    /**
     * 新增法律法规指导文件
     */
    public SpcyFgZdwj addZdwj(SpcyFgZdwj zdwj){
        zdwj.setId(GetUuIdUtils.ReplaceUuId());
        if (zdwj.getForder() == null){
            zdwj.setForder(this.getZdwjNumber()+1);
        }
        spcyFgZdwjService.save(zdwj);

        String lognr = "新增法律法规指定文件: ";
        lognr += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(zdwj));

        spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,lognr,zdwj.getId(),"新增法律法规指定文件",SpcyConstant.LOGS_SYS_TYPE_XZ);

        return zdwj;
    }

    /**
     * 更新法律法规指导文件
     * @param map (id)
     */
    public void updateZdwj(HashMap<String,Object> map){
        String id = map.get("id").toString();

        SpcyFgZdwj update = spcyFgZdwjService.getById(id);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(update,map);
        } catch (IllegalAccessException e) {
            log.error("更新法律法规指导文件出错：【IllegalAccessException】-> {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("更新法律法规指导文件出错：【InvocationTargetException】-> {}",e.getMessage());
        }

        spcyFgZdwjService.updateById(update);

        String logNr = "更新法律法规指导文件: ";
        logNr += LogUtils.BeanModifyMsg(update,map);

        spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,logNr,id,"更新法律法规指导文件", SpcyConstant.LOGS_SYS_TYPE_XG);
    }

    /**
     * 删除法律法规指导文件
     * @param id 指导文件id
     */
    public void deleteZdwj(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("请选择数据！");
        }
        List<String> idsList = StringPatternUtils.getListByIds(id);
        List<SpcyFgZdwj> zdwjList = spcyFgZdwjService.listByIds(idsList);

        if (CollectionUtil.isNotEmpty(zdwjList)) {
            for (SpcyFgZdwj zdwj : zdwjList) {
                spcyFgZdwjService.removeById(zdwj.getId());

                String lognr = "删除法律法规指导文件：";
                lognr += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(zdwj));

                spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,lognr,zdwj.getId(),"删除法律法规指导文件", SpcyConstant.LOGS_SYS_TYPE_SC);
            }
        }
    }


    /******************************************【 法规名称 】*****************************************************/

    /**
     * 获取对应指导文件下存在的法规名称数量
     */
    private int getZdwjIndexFgmcNumber(String zdwjid){
        List<SpcyFgFgmc> countList = spcyFgFgmcService.list(new LambdaQueryWrapper<SpcyFgFgmc>()
                .eq(SpcyFgFgmc::getFzdwjid, zdwjid));

        return countList.size();
    }

    /**
     * 获取对应指定文件下的所有法规列表
     * @param zdwjid
     * @return
     */
    private List<SpcyFgFgmc> queryFgListByZdwjId(String zdwjid){
        List<SpcyFgFgmc> list = spcyFgFgmcService.list(new LambdaQueryWrapper<SpcyFgFgmc>()
                .eq(SpcyFgFgmc::getFzdwjid, zdwjid)
                .orderByAsc(SpcyFgFgmc::getForder));

        return list;
    }

    /**
     * 查询法规-更具法规id
     */
    private SpcyFgFgmc queryFgById(String id){
         return spcyFgFgmcService.getById(id);
    }

    /**
     * 新增法律法规法规名称
     */
    public SpcyFgFgmc addFgmc(SpcyFgFgmc fgmc){
        if (StringUtils.isBlank(fgmc.getFzdwjid())){
            throw new BusinessException("请选择指导文件参数！");
        }
        fgmc.setId(GetUuIdUtils.ReplaceUuId());
        fgmc.setForder(this.getZdwjIndexFgmcNumber(fgmc.getFzdwjid())+1);
        spcyFgFgmcService.save(fgmc);

        String lognr = "新增法律法规法规名称: ";
        lognr += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(fgmc));

        spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,lognr,fgmc.getFzdwjid()+"/"+fgmc.getId(),"新增法律法规法规名称",SpcyConstant.LOGS_SYS_TYPE_XZ);

        return fgmc;
    }

    /**
     * 更新法律法规法规名称
     * @param map (id)
     */
    public void updateFgmc(HashMap<String,Object> map){
        String id = map.get("id").toString();

        SpcyFgFgmc update = spcyFgFgmcService.getById(id);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(update,map);
        } catch (IllegalAccessException e) {
            log.error("更新法律法规法规名称出错：【IllegalAccessException】-> {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("更新法律法规法规名称出错：【InvocationTargetException】-> {}",e.getMessage());
        }

        spcyFgFgmcService.updateById(update);

        String logNr = "更新法律法规法规名称: ";
        logNr += LogUtils.BeanModifyMsg(update,map);

        spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,logNr,update.getFzdwjid()+"/"+id,"更新法律法规法规名称", SpcyConstant.LOGS_SYS_TYPE_XG);
    }

    /**
     * 删除法律法规法规名称
     * @param id 法规id
     */
    public void deleteFgmc(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("请选择数据！");
        }
        List<String> idsList = StringPatternUtils.getListByIds(id);
        List<SpcyFgFgmc> fgmcList = spcyFgFgmcService.listByIds(idsList);

        if (CollectionUtil.isNotEmpty(fgmcList)) {
            for (SpcyFgFgmc fgmc : fgmcList) {
                spcyFgFgmcService.removeById(fgmc.getId());

                String lognr = "删除法律法规法规名称：";
                lognr += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(fgmc));

                spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,lognr,fgmc.getFzdwjid()+"/"+fgmc.getId(),"删除法律法规法规名称", SpcyConstant.LOGS_SYS_TYPE_SC);
            }
        }
    }

    /******************************************【 章节目录 】*****************************************************/

    /**
     * 获取对应法律法规名称下存在的章节数量
     * @param fgid 法规id
     */
    private int getFgmcIndexZjmlNumber(String fgid){
        List<SpcyFgZjml> countList = spcyFgZjmlService.list(new LambdaQueryWrapper<SpcyFgZjml>()
                .eq(SpcyFgZjml::getFfgid, fgid));

        return countList.size();
    }

    /**
     * 获取对应父章节目录存在数量
     * @param fzjid 父章节id
     */
    private int fatherZjmlNumber(String fzjid){
        List<SpcyFgZjml> countList = spcyFgZjmlService.list(new LambdaQueryWrapper<SpcyFgZjml>()
                .eq(SpcyFgZjml::getFfzjid, fzjid));

        return countList.size();
    }

    /**
     * 获取对应法规的章节列表
     * @param fgid
     * @return
     */
    private List<SpcyFgZjml> queryZjListByFgId(String fgid){
        List<SpcyFgZjml> list = spcyFgZjmlService.list(new LambdaQueryWrapper<SpcyFgZjml>()
                .eq(SpcyFgZjml::getFfgid, fgid)
                .orderByAsc(SpcyFgZjml::getForder));
        return list;
    }

    /**
     * 获取对应法规的章节列表
     * @param fgid
     * @return
     */
    private List<SpcyFgZjml> queryZjListByFgIdAndGjz(String fgid, String gjz){
        List<SpcyFgZjml> list = spcyFgZjmlService.list(new LambdaQueryWrapper<SpcyFgZjml>()
                .eq(SpcyFgZjml::getFfgid, fgid)
                .like(SpcyFgZjml::getFzjmc, gjz)
                .orderByAsc(SpcyFgZjml::getForder));
        return list;
    }

    /**
     * 获取对应法规的章节父级列表
     * @param fgid
     * @return
     */
    private List<SpcyFgZjml> queryZjParentListByFgId(String fgid){
        List<SpcyFgZjml> list = spcyFgZjmlService.list(new LambdaQueryWrapper<SpcyFgZjml>()
                .eq(SpcyFgZjml::getFfgid, fgid)
                .isNull(SpcyFgZjml::getFfzjid)
                .orderByAsc(SpcyFgZjml::getForder));
        return list;
    }

    /**
     * 获取对应章节目录-根据章节id
     * @param zjid
     * @return
     */
    public SpcyFgZjml queryZdmlById(String zjid){
        SpcyFgZjml zjml = spcyFgZjmlService.getById(zjid);

        return zjml;
    }

    /**
     * 验证当前章节目录是否存在下级目录，存在则抛出问题
     * @param zjid
     */
    private void verifyZdmlIsExistSub(String zjid){
        List<SpcyFgZjml> list = spcyFgZjmlService.list(new LambdaQueryWrapper<SpcyFgZjml>()
                .eq(SpcyFgZjml::getFfzjid, zjid));

        if (CollectionUtil.isNotEmpty(list)){
            throw new BusinessException("当前章节存在下级，无法删除！请先删除对应的下级目录！");
        }
    }

    /**
     * 新增法律法规章节目录
     */
    public SpcyFgZjml addZjml(SpcyFgZjml zjml){
        if (StringUtils.isBlank(zjml.getFfgid())){
            throw new BusinessException("请选择法规参数！");
        }
        zjml.setId(GetUuIdUtils.ReplaceUuId());
        if (zjml.getForder() == null) {
            zjml.setForder(this.getFgmcIndexZjmlNumber(zjml.getFfgid()));
        }
        spcyFgZjmlService.save(zjml);

        String lognr = "新增法律法规章节目录: ";
        lognr += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(zjml));

        spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,lognr,zjml.getFfgid(),"新增法律法规章节目录",SpcyConstant.LOGS_SYS_TYPE_XZ);

        return zjml;
    }

    /**
     * 新增法律法规章节目录-新增下级
     */
    public SpcyFgZjml addZjmlInSub(SpcyFgZjml zjml){
        /*if (StringUtils.isBlank(zjml.getFfgid())){
            throw new BusinessException("请选择法规名称参数！");
        }*/
        if (StringUtils.isBlank(zjml.getFfzjid())){
            throw new BusinessException("请选择父章节目录参数！");
        }
        if (StringUtils.isBlank(zjml.getFfgid())){
            SpcyFgZjml fzj = this.queryZdmlById(zjml.getFfzjid());
            if (fzj == null){
                throw new BusinessException("添加下级章节失败！当前父级章节不存在！");
            }
            zjml.setFfgid(fzj.getFfgid());
        }
        zjml.setId(GetUuIdUtils.ReplaceUuId());
        if (zjml.getForder() == null) {
            zjml.setForder(this.fatherZjmlNumber(zjml.getFfzjid()));
        }
        spcyFgZjmlService.save(zjml);

        String lognr = "新增法律法规章节目录下级: ";
        lognr += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(zjml));

        spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,lognr,zjml.getFfgid(),"新增法律法规章节目录下级",SpcyConstant.LOGS_SYS_TYPE_XZ);

        return zjml;
    }

    /**
     * 更新法律法规章节目录
     * @param map (id)
     */
    public void updateZjml(HashMap<String,Object> map){
        String id = map.get("id").toString();

        SpcyFgZjml update = spcyFgZjmlService.getById(id);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(update,map);
        } catch (IllegalAccessException e) {
            log.error("更新法律法规章节目录出错：【IllegalAccessException】-> {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("更新法律法规章节目录出错：【InvocationTargetException】-> {}",e.getMessage());
        }

        spcyFgZjmlService.updateById(update);

        String logNr = "更新法律法规章节目录: ";
        logNr += LogUtils.BeanModifyMsg(update,map);

        spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,logNr,update.getFfgid(),"更新法律法规章节目录", SpcyConstant.LOGS_SYS_TYPE_XG);
    }

    /**
     * 删除法律法规章节目录
     * @param id 章节id
     */
    public void deleteZjml(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("请选择数据！");
        }

        SpcyFgZjml zjml = spcyFgZjmlService.getById(id);
        if (zjml == null){
            throw new BusinessException("当前章节不存在！请刷新重试！");
        }
        this.verifyZdmlIsExistSub(zjml.getId());  // 验证是否存在下级，存在不给删除
        spcyFgZjmlService.removeById(zjml.getId());

        String lognr = "删除法律法规章节目录：";
        lognr += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(zjml));
        spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,lognr,zjml.getFfgid(),"删除法律法规章节目录", SpcyConstant.LOGS_SYS_TYPE_SC);
    }

    /******************************************【 条款 】*****************************************************/

    /**
     * 获取对应章节目录下存在的条款数量
     */
    private int getZjmlIndexTkNumber(String zjid){
        List<SpcyFgTk> countList = spcyFgTkService.list(new LambdaQueryWrapper<SpcyFgTk>()
                .eq(SpcyFgTk::getFzjid, zjid));

        return countList.size();
    }

    /**
     * 根据条款id-获取对应的条款信息
     * @param tkid 条款id
     */
    private SpcyFgTk queryTkById(String tkid){
        SpcyFgTk tk = spcyFgTkService.getById(tkid);

        return tk;
    }

    /**
     * 根据章节id-获取条款列表
     */
    private List<SpcyFgTk> queryTkListByZjId(String zjid){
        List<SpcyFgTk> tkList = spcyFgTkService.list(new LambdaQueryWrapper<SpcyFgTk>()
                .eq(SpcyFgTk::getFzjid, zjid)
                .orderByAsc(SpcyFgTk::getForder));

        return tkList;
    }

    /**
     * 新增法律法规条款
     */
    public SpcyFgTk addTk(SpcyFgTk tk){
        if (StringUtils.isBlank(tk.getFzjid())){
            throw new BusinessException("请选择章节参数！");
        }
        /*if (StringUtils.isBlank(tk.getFfgid())){
            throw new BusinessException("请选择法规名称参数！");
        }*/

        SpcyFgZjml zj = this.queryZdmlById(tk.getFzjid());
        if (zj == null){
            throw new BusinessException("当前选择的章节不存在！");
        }
        tk.setFfgid(zj.getFfgid());

        tk.setId(GetUuIdUtils.ReplaceUuId());

        if (tk.getForder() == null) {
            tk.setForder(this.getZjmlIndexTkNumber(tk.getFzjid())+1);
        }
        spcyFgTkService.save(tk);

        // 添加索引条款
        if (isEnableIndexTk) {

        }

        String lognr = "新增法律法规条款: ";
        lognr += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(tk));

        spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,lognr,tk.getFfgid(),"新增法律法规条款",SpcyConstant.LOGS_SYS_TYPE_XZ);

        return tk;
    }

    /**
     * 更新法律法规条款
     * @param map (id)
     */
    public void updateTk(HashMap<String,Object> map){
        String id = map.get("id").toString();

        SpcyFgTk update = spcyFgTkService.getById(id);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(update,map);
        } catch (IllegalAccessException e) {
            log.error("更新法律法规章节目录出错：【IllegalAccessException】-> {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("更新法律法规章节目录出错：【InvocationTargetException】-> {}",e.getMessage());
        }

        spcyFgTkService.updateById(update);

        // 删除索引条款
        if (isEnableIndexTk) {

        }

        String logNr = "更新法律法规条款: ";
        logNr += LogUtils.BeanModifyMsg(update,map);

        spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,logNr,update.getFfgid(),"更新法律法规条款", SpcyConstant.LOGS_SYS_TYPE_XG);
    }

    /**
     * 删除法律法规条款
     * @param id 条款id
     */
    public void deleteTk(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("请选择数据！");
        }

        SpcyFgTk tk = spcyFgTkService.getById(id);
        if (tk == null){
            throw new BusinessException("当前条款不存在！请刷新重试！");
        }
        spcyFgTkService.removeById(tk.getId());

        // 删除索引条款
        if (isEnableIndexTk) {

        }

        String lognr = "删除法律法规条款：";
        lognr += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(tk));
        spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,lognr,tk.getFfgid(),"删除法律法规条款", SpcyConstant.LOGS_SYS_TYPE_SC);
    }

    /******************************************【 指导原则 】*****************************************************/

    /**
     * 获取对应章节目录下存在的指导原则数量
     */
    private int getZjmlIndexZdyzNumber(String zjid){
        List<SpcyFgZdyz> countList = spcyFgZdyzService.list(new LambdaQueryWrapper<SpcyFgZdyz>()
                .eq(SpcyFgZdyz::getFzjid, zjid));

        return countList.size();
    }

    /**
     * 获取对应的指导原则-根据指导原则id
     */
    private SpcyFgZdyz queryZdyzListById(String id){
        SpcyFgZdyz zdyz = spcyFgZdyzService.getById(id);

        return zdyz;
    }

    /**
     * 新增法律法规指导原则
     */
    public SpcyFgZdyz addZdyz(SpcyFgZdyz zdyz){
        /*if (StringUtils.isBlank(zdyz.getFzjid())){
            throw new BusinessException("请选择章节参数！");
        }*/
        /*if (StringUtils.isBlank(zdyz.getFfgid())){
            throw new BusinessException("请选择法规名称参数！");
        }*/

        if (StringUtils.isBlank(zdyz.getFtkid())){
            throw new BusinessException("请选择条款参数！");
        }

        SpcyFgTk tk = this.queryTkById(zdyz.getFtkid());
        if (tk == null){
            throw new BusinessException("当前选择的条款不存在！");
        }
        zdyz.setFzjid(tk.getFzjid());
        zdyz.setFfgid(tk.getFfgid());

        zdyz.setId(GetUuIdUtils.ReplaceUuId());
        if (zdyz.getForder() == null) {
            zdyz.setForder(this.getZjmlIndexZdyzNumber(zdyz.getFzjid()));
        }
        spcyFgZdyzService.save(zdyz);

        String lognr = "新增法律法规指导原则: ";
        lognr += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(zdyz));

        spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,lognr,zdyz.getFfgid(),"新增法律法规指导原则",SpcyConstant.LOGS_SYS_TYPE_XZ);

        return zdyz;
    }

    /**
     * 更新法律法规指导原则
     * @param map (id)
     */
    public void updateZdyz(HashMap<String,Object> map){
        String id = map.get("id").toString();

        SpcyFgZdyz update = spcyFgZdyzService.getById(id);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(update,map);
        } catch (IllegalAccessException e) {
            log.error("更新法律法规指导原则出错：【IllegalAccessException】-> {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("更新法律法规指导原则出错：【InvocationTargetException】-> {}",e.getMessage());
        }

        spcyFgZdyzService.updateById(update);

        String logNr = "更新法律法规指导原则: ";
        logNr += LogUtils.BeanModifyMsg(update,map);

        spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,logNr,update.getFfgid(),"更新法律法规指导原则", SpcyConstant.LOGS_SYS_TYPE_XG);
    }

    /**
     * 删除法律法规指导原则
     * @param id 指导原则id
     */
    public void deleteZdyz(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("请选择数据！");
        }

        SpcyFgZdyz zdyz = spcyFgZdyzService.getById(id);
        if (zdyz == null){
            throw new BusinessException("当前指导原则不存在！请刷新重试！");
        }
        spcyFgTkService.removeById(zdyz.getId());

        String lognr = "删除法律法规指导原则：";
        lognr += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(zdyz));
        spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,lognr,zdyz.getFfgid(),"删除法律法规指导原则", SpcyConstant.LOGS_SYS_TYPE_SC);
    }

    /******************************************【 描述模版 】*****************************************************/

    /**
     * 新增法律法规描述模版
     */
    public SpcyFgZjmsmb addMsmb(SpcyFgZjmsmb msmb){
        if (StringUtils.isBlank(msmb.getFzjid())){
            throw new BusinessException("请选择章节参数！");
        }
        SpcyFgZjml zjml = this.queryZdmlById(msmb.getFzjid());
        if (zjml == null){
            throw new BusinessException("当前章节不存在！请刷新重试！");
        }
        msmb.setFzjmc(zjml.getFzjmc());
        msmb.setId(GetUuIdUtils.ReplaceUuId());
        spcyFgZjmsmbService.save(msmb);

        String lognr = "新增法律法规描述模版: ";
        lognr += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(msmb));

        spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,lognr,msmb.getFzjid(),"新增法律法规描述模版",SpcyConstant.LOGS_SYS_TYPE_XZ);

        return msmb;
    }

    /**
     * 更新法律法规描述模版
     * @param map (id)
     */
    public void updateMsmb(HashMap<String,Object> map){
        String id = map.get("id").toString();

        SpcyFgZjmsmb update = spcyFgZjmsmbService.getById(id);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(update,map);
        } catch (IllegalAccessException e) {
            log.error("更新法律法规描述模版出错：【IllegalAccessException】-> {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("更新法律法规描述模版出错：【InvocationTargetException】-> {}",e.getMessage());
        }

        spcyFgZjmsmbService.updateById(update);

        String logNr = "更新法律法规描述模版: ";
        logNr += LogUtils.BeanModifyMsg(update,map);

        spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,logNr,update.getFzjid(),"更新法律法规描述模版", SpcyConstant.LOGS_SYS_TYPE_XG);
    }

    /**
     * 删除法律法规描述模版
     * @param id 模版id
     */
    public void deleteMsmb(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("请选择数据！");
        }

        SpcyFgZjmsmb msmb = spcyFgZjmsmbService.getById(id);
        if (msmb == null){
            throw new BusinessException("当前模版不存在！请刷新重试！");
        }
        spcyFgZjmsmbService.removeById(msmb.getId());

        String lognr = "删除法律法规描述模版：";
        lognr += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(msmb));
        spcyLogService.addLogs(LogConstant.LOGS_FLFG_GL,lognr,msmb.getFzjid(),"删除法律法规描述模版", SpcyConstant.LOGS_SYS_TYPE_SC);
    }

    /**
     * 根据法规id-将章节数组列表转为树形列表
     * @param fgid
     * @return
     */
    private List<FlfgZjmlTreeVo> initZjTree(String fgid, String parentid){
        return this.initZjTree(fgid, parentid, "");
    }
    private List<FlfgZjmlTreeVo> initZjTree(String fgid, String parentid, String gjz){

        List<SpcyFgZjml> zjList = this.queryZjListByFgId(fgid);
        if (StringUtils.isBlank(gjz)) {
            zjList = this.queryZjListByFgId(fgid);
        }else {
            /** 法规id和条款关键字查询章节 */
            zjList = spcyFgZjmlMapper.selectZjmlByGjzAndFfgid(fgid, gjz);

            /** 为空再进行章节关键查找 */
            if (CollectionUtil.isEmpty(zjList)){
                zjList = this.queryZjListByFgIdAndGjz(fgid, gjz);
            }
        }

        // 树形章节列表
        List<FlfgZjmlTreeVo> zjTreeList = new ArrayList<>();

        // 对象转换vo
        if (CollectionUtil.isNotEmpty(zjList)) {
            zjList.forEach(bean -> {
                FlfgZjmlTreeVo treeVo = new FlfgZjmlTreeVo();
                try {
//                    ConvertUtils.register(new DateConvert(), java.util.Date.class);
//                    BeanUtils.copyProperties(treeVo, bean);
                    BeanUtil.copyProperties(bean, treeVo);
                    treeVo.setKey(treeVo.getId());
                    treeVo.setTitle(treeVo.getFzjmc());
                    treeVo.setParentid(parentid);

                    zjTreeList.add(treeVo);
                } catch (Exception e) {
                    log.error("将章节数组列表转为树形列表-出错[IllegalAccessException]：{}", e.getMessage());
                }
            });

            // 双重循环
            for (FlfgZjmlTreeVo t : zjTreeList) {
                // 每次循环找到每个节点的子节点们（f代表符合条件的集合），并挂载到当前节点下
                List<FlfgZjmlTreeVo> children = zjTreeList.stream().filter(f -> t.getId().equals(f.getFfzjid())).collect(Collectors.toList());
                t.setChildren(children);
            }

            // for循环结束，整棵树挂载完毕，需要找到树的根节点（定义ffzjid为null就是根节点）
            List<FlfgZjmlTreeVo> treeList = zjTreeList.stream()
                    .filter(f -> StringUtils.isBlank(f.getFfzjid())).collect(Collectors.toList());

            return treeList;
        } else {
            return new ArrayList<>();
        }

    }

    /** 条款章节 */
    private List<FlfgZjmlTreeVo> initTkTree(String fgid, String parentid, String gjz){
        List<SpcyFgZjml> zjList = this.queryZjListByFgId(fgid);
        if (StringUtils.isBlank(gjz)) {
            zjList = this.queryZjListByFgId(fgid);
        }else {
            /** 法规id和条款关键字查询章节 */
            zjList = spcyFgZjmlMapper.selectZjmlByGjzAndFfgid(fgid, gjz);

            /** 为空再进行章节关键查找 */
            if (CollectionUtil.isEmpty(zjList)){
                zjList = this.queryZjListByFgIdAndGjz(fgid, gjz);
            }
        }

        // 树形章节列表
        List<FlfgZjmlTreeVo> zjTreeList = new ArrayList<>();

        // 对象转换vo
        if (CollectionUtil.isNotEmpty(zjList)) {
            zjList.forEach(bean -> {
                FlfgZjmlTreeVo treeVo = new FlfgZjmlTreeVo();
                BeanUtil.copyProperties(bean, treeVo);

                treeVo.setKey(treeVo.getId());
                treeVo.setTitle(treeVo.getFzjmc());
                treeVo.setParentid(parentid);

                /**
                 * 条款
                 */
                List<FlfgZjmlTreeVo> tkTreeList = new ArrayList<>();
                LambdaQueryWrapper<SpcyFgTk> queryTkWrapper = new LambdaQueryWrapper<>();
                queryTkWrapper.eq(SpcyFgTk::getFzjid, bean.getId());
                if (StringUtils.isNotBlank(gjz)){
                    queryTkWrapper.and(q->
                            q.like(SpcyFgTk::getFzdyzh, gjz)
                            .or()
                            .like(SpcyFgTk::getFzdyznr, gjz)
                            .or()
                            .like(SpcyFgTk::getFjcyd, gjz)
                    );
                }
                queryTkWrapper.orderByAsc(SpcyFgTk::getForder);

                List<SpcyFgTk> tkList = spcyFgTkService.list(queryTkWrapper);
                for (SpcyFgTk tk : tkList){
                    FlfgZjmlTreeVo tkTreeVo = new FlfgZjmlTreeVo();

                    tkTreeVo.setId(tk.getId());
                    tkTreeVo.setKey(tk.getId());

                    tkTreeVo.setFzjmc(tk.getFzdyzh());
                    tkTreeVo.setFzjjc(tk.getFzdyzh());
                    tkTreeVo.setTitle(tk.getFzdyzh());

                    tkTreeVo.setForder(tk.getForder());

                    tkTreeVo.setParentid(bean.getId());

                    tkTreeList.add(tkTreeVo);
                }

                treeVo.setChildren(tkTreeList);

                zjTreeList.add(treeVo);
            });



            return zjTreeList;
        } else {
            return new ArrayList<>();
        }
    }

    /******************************************【 现场检查章节 】*****************************************************/

    /**
     * 验证现场检查章节是否已经初始化-根据申请事项id
     * @param sqsxid 申请事项id
     * @return true: 已经初始化
     */
    private boolean verifyIsInitXcjcZj(String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
            throw new BusinessException("申请事项参数为空！");
        }
        List<SpcyFgXcjczj> list = spcyFgXcjczjService.list(new LambdaQueryWrapper<SpcyFgXcjczj>()
                .eq(SpcyFgXcjczj::getFsqsxid, sqsxid));
        if (CollectionUtil.isNotEmpty(list)){
            return true;
        }

        return false;
    }

    /**
     * 初始化现场检查章节-根据法律法规库
     */
    @Async("asyncNormalTask")
    public void initXcjczj(SpcySqsxBasic sqsx, String sqsxid){
        if (sqsx == null){
            log.error("初始化现场检查章节失败: 申请事项不存在！申请事项id【{}】;编码：{}", sqsxid, sqsx.getFywflcode());
        }

        if (StringUtils.isBlank(sqsx.getFywflcode())){
            log.error("初始化现场检查章节失败：业务分类编码为空！申请事项id【{}】", sqsxid);
            return;
        }

        /*// 填写负责人
        String txfzrid = "";
        if (CollectionUtil.isNotEmpty(zzList)){
            txfzrid = zzList.get(0).getFuserid();
        }*/

        // 药品生产
        if (sqsx.getFywflcode().equals(SpcyConstant.YWFL_CODE_YPSC)){
            this.initXcjcZjInYpsc(sqsx.getId(), sqsx.getFqyid(), null);
        }

    }

    /**
     * 初始化现场检查章节-根据文件模板章节
     */
    @Async("asyncNormalTask")
    public void initWjmbZjGenXcjczj(List<SpcyRsWjmbZjml> wjmbZjmlList, SpcySqsxBasic sqsx, String sqsxid){
        // 验证对应法规是否已经初始化
        if (verifyIsInitXcjcZj(sqsxid)) {
            return;
        }

        List<SpcyFgXcjczj> xcjczjList = new ArrayList<>();

//        boolean isFpzt = false;

        String wjmbId = "";
        if (CollectionUtil.isNotEmpty(wjmbZjmlList)) {
            for (SpcyRsWjmbZjml wjmbZjml : wjmbZjmlList) {
                SpcyFgXcjczj xcjczj = new SpcyFgXcjczj();
                xcjczj.setId(GetUuIdUtils.NotReplaceUuId());
                xcjczj.setForder(wjmbZjml.getForder());

                xcjczj.setFzjmc("（" + ChineseNumberConverterUtils.convertToChinese(wjmbZjml.getForder()) + "）" + wjmbZjml.getFzjmc());

                xcjczj.setFsqsxid(sqsxid);
                xcjczj.setFqyid(sqsx.getFqyid());

                xcjczj.setFtxfzrid(Optional.ofNullable(wjmbZjml.getFtxfzrid()).orElseGet(()->""));
                /*if (!isFpzt && StringUtils.isNotBlank(xcjczj.getFtxfzrid())){
                    isFpzt = true;
                }*/

                xcjczj.setFwczt(SpcyCommonConstant.FLFG_XCJCZJ_WCZT_WWC);
                xcjczj.setFdycs(0);
                xcjczj.setFmsmbnr(wjmbZjml.getFmsmbnr());
                xcjczj.setFjcms(wjmbZjml.getFmsmbnr());

                xcjczj.setCreateTime(new Date());
                xcjczj.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());

                xcjczjList.add(xcjczj);

                wjmbId = wjmbZjml.getFwjmbid();
            }
        }
        else {
            List<String> zjList = StringPatternUtils.getListByIds(spcyRsYwzdxxService.querySqsxDefaultJczj(sqsx.getFywflcode()));

            for (int i = 0; i < zjList.size(); i++){
                SpcyFgXcjczj xcjczj = new SpcyFgXcjczj();
                xcjczj.setId(GetUuIdUtils.NotReplaceUuId());
                xcjczj.setForder(i+1);

                xcjczj.setFzjmc("（" + ChineseNumberConverterUtils.convertToChinese(i+1) + "）" + zjList.get(i));

                xcjczj.setFsqsxid(sqsxid);
                xcjczj.setFqyid(sqsx.getFqyid());

                xcjczj.setFwczt(SpcyCommonConstant.FLFG_XCJCZJ_WCZT_WWC);
                xcjczj.setFdycs(0);

                xcjczj.setCreateTime(new Date());
                xcjczj.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());

                xcjczjList.add(xcjczj);
            }
        }

        spcyFgXcjczjService.saveBatch(xcjczjList);

        /*if (isFpzt){
            spcySqsxBaiscService.updateSqsxRwfpState(sqsxid);
        }*/

        /** 同步检查情况转述 */
        try {
            if (StringUtils.isNotBlank(wjmbId)) {
                String msmb = spcyFgZjmlMapper.selectWjmbMbnrByWjmbId(wjmbId);
                if (StringUtils.isNotBlank(msmb)) {
                    Map<String, String> templateMap = spcyTemplateService.genCommonTemplateMap(sqsxid, null);
                    msmb = StringReplaceMapUtils.replaceWithMap(msmb, templateMap);

                    LambdaUpdateWrapper<SpcySqsxBasic> updateWrapper = new LambdaUpdateWrapper<>();
                    updateWrapper.eq(SpcySqsxBasic::getId, sqsxid);

                    SpcySqsxBasic updateBean = new SpcySqsxBasic();
                    updateBean.setFjcqkzs(msmb);
                    spcySqsxBaiscService.update(updateBean, updateWrapper);
                }
            }
        }catch (Exception e){
            log.error("同步检查情况转述-错误：{}",e.getMessage());
        }


    }

    /**
     * 初始化现场检查章节-根据文件模板章节
     */
    @Async("asyncNormalTask")
    public void initWjmbZjGenXcjczjInHbjc(List<SpcyRsWjmbZjml> wjmbZjmlList, SpcySqsxBasic sqsx, String sqsxid){
        this.initWjmbZjGenXcjczj(null, sqsx, sqsxid);
    }

    /**
     * 合并检查事项-现场检查-章节、问题和条款同步
     * @param bhbsqsxid 被更新的id
     */
//    @Async("asyncNormalTask")
    public void syncHbsqsxInfoZjAndWtAndTk(String sqsxid, String bhbsqsxid){
        List<SpcyFgXcjczj> zjList = spcyFgXcjczjService.list(new LambdaQueryWrapper<SpcyFgXcjczj>()
                .eq(SpcyFgXcjczj::getFsqsxid, sqsxid));
        if (CollectionUtil.isEmpty(zjList)){
            return;
        }

        /** 删除已有的事项章节 */
        spcyFgXcjczjService.remove(new LambdaQueryWrapper<SpcyFgXcjczj>().eq(SpcyFgXcjczj::getFsqsxid, bhbsqsxid));

        List<SpcyFgXcjczj> newZjList = new ArrayList<>();
        List<SpcyFgXcjcwt> newWtList = new ArrayList<>();
        List<SpcyFgXcjcwttk> newWtTkList = new ArrayList<>();
        for (SpcyFgXcjczj oldZj : zjList){
            SpcyFgXcjczj newZj = new SpcyFgXcjczj();
            BeanUtil.copyProperties(oldZj, newZj);
            newZj.setId(GetUuIdUtils.NotReplaceUuId());
            newZj.setFsqsxid(bhbsqsxid);

            newZjList.add(newZj);

            List<SpcyFgXcjcwt> wtList = spcyFgXcjcwtService.list(new LambdaQueryWrapper<SpcyFgXcjcwt>()
                    .eq(SpcyFgXcjcwt::getFxcjczjid, oldZj.getId()));
            for (SpcyFgXcjcwt oldWt : wtList){
                SpcyFgXcjcwt newWt = new SpcyFgXcjcwt();
                BeanUtil.copyProperties(oldWt, newWt);
                newWt.setId(GetUuIdUtils.NotReplaceUuId());
                newWt.setFsqsxid(bhbsqsxid);
                newWt.setFxcjczjid(newZj.getId());

                newWtList.add(newWt);

                List<SpcyFgXcjcwttk> wttkList = spcyFgXcjcwttkService.list(new LambdaQueryWrapper<SpcyFgXcjcwttk>()
                        .eq(SpcyFgXcjcwttk::getFxcjcwtid, oldWt.getId()));
                for (SpcyFgXcjcwttk oldTk : wttkList){
                    SpcyFgXcjcwttk newTk = new SpcyFgXcjcwttk();
                    BeanUtil.copyProperties(oldTk, newTk);
                    newTk.setId(GetUuIdUtils.NotReplaceUuId());
                    newTk.setFsqsxid(bhbsqsxid);
                    newTk.setFxcjcwtid(newWt.getId());

                    newWtTkList.add(newTk);
                }
            }
        }

        spcyFgXcjczjService.saveBatch(newZjList);
        spcyFgXcjcwtService.saveBatch(newWtList);
        spcyFgXcjcwttkService.saveBatch(newWtTkList);
    }

    /**
     * 初始化现场检查章节-药品生产
     * @param sqsxid 申请事项id
     * @param fqyid 企业id
     * @param txfzrid 填写负责人id
     */
    public void initXcjcZjInYpsc(String sqsxid,String fqyid, String txfzrid){
        // 验证对应法规是否已经初始化
        if (verifyIsInitXcjcZj(sqsxid)) {
            return;
        }

        // 项目初始化：比如药品生产 固定取法规药品GMP对应的所有章节作为检查项目
        List<SpcyFgFgmc> fgList = this.queryFgListByZdwjId(SpcyCommonConstant.YPSC_ZDWJ_ID);
        if (CollectionUtil.isEmpty(fgList)){
            log.error("初始化现场检查章节失败，指导文件【{}】,不存在对应的法规", SpcyCommonConstant.YPSC_ZDWJ_ID);
            return;
        }

        for (int order = 0; order < fgList.size(); order++) {
            this.initXcjcZj(fgList.get(order).getId(), sqsxid, fqyid, txfzrid);
        }
    }

    // 初始化现场检查章节
    private void initXcjcZj(String fgid, String sqsxid, String fqyid, String txfzrid){
        // 获取对应法规的所有章节
        List<SpcyFgZjml> zjList = queryZjListByFgId(fgid);

        List<SpcyFgXcjczj> xcjczjList = new ArrayList<>();

        int order = 1;
        for (int n=0; n<zjList.size(); n++){
            SpcyFgZjml zjml = zjList.get(n);

            if (StringUtils.isNotBlank(zjml.getFzjmc()) && zjml.getFzjmc().contains("附则")){
                continue;
            }

            if (StringUtils.isNotBlank(zjml.getFzjjc()) && zjml.getFzjjc().contains("附则")){
                continue;
            }

            // 不存在父章节
            if (StringUtils.isBlank(zjml.getFfzjid())) {
                xcjczjList.add(this.generateXcjcZj(zjml, sqsxid, fqyid, order, fgid, txfzrid));
                order++;
            }
            // 获取子级章节
            List<SpcyFgZjml> children = zjList.stream()
                    .filter(f -> zjml.getId().equals(f.getFfzjid()))
                    .collect(Collectors.toList());
            for (int m = 0; m < children.size(); m++) {
                SpcyFgZjml zjml2 = zjList.get(m);

                // 不存在父章节
                if (StringUtils.isBlank(zjml2.getFfzjid())) {
                    xcjczjList.add(this.generateXcjcZj(zjml2, sqsxid, fqyid, order, fgid, txfzrid));
                    order++;
                }

                // 获取子级章节
                List<SpcyFgZjml> children2 = zjList.stream()
                        .filter(f -> zjml2.getId().equals(f.getFfzjid()))
                        .collect(Collectors.toList());
                for (int m2 = 0; m2 < children2.size(); m2++) {
                    // 不存在父章节
                    if (StringUtils.isBlank(children2.get(m2).getFfzjid())) {
                        xcjczjList.add(this.generateXcjcZj(children2.get(m2), sqsxid, fqyid, order, fgid, txfzrid));
                        order++;
                    }
                }
            }

        }

//        spcyFgXcjczjService.saveBatch(xcjczjList);
        spcyFgXcjczjService.batchInsertList(xcjczjList);
    }

    /**
     * 验证事项-所有问题是否都已经选择条款
     */
    public void verifyXcjcWtAndTkEmpty(String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
            return;
        }

        List<XcjcMsWtTkVo> list = spcyFgZjmlMapper.selectZjAndWtAndTkBySqsxId(sqsxid);
        if (CollectionUtil.isEmpty(list)){
            return;
        }

        String resultError = "";
        String errorContent = "";
        String errorTitle = "";
        for (XcjcMsWtTkVo bean : list){
            errorTitle = "";
            errorContent = "";

            if (StringUtils.isBlank(bean.getFjcms())){
                errorContent += "请填写[章节的描述]; ";
                errorTitle = bean.getFzjmc();
            }
            if (StringUtils.isBlank(bean.getFyzcd())){
                errorContent += "请填写";
                errorContent += "[问题"+bean.getForder()+"]";
                errorContent += "缺陷类型; ";

                errorTitle = bean.getFzjmc();
            }
            if (StringUtils.isBlank(bean.getFwtms())){
                errorContent += "请填写";
                errorContent += "[问题"+bean.getForder()+"]";
                errorContent += "的描述; ";

                errorTitle = bean.getFzjmc();
            }
            if (StringUtils.isBlank(bean.getFfgtk())){
                errorContent += "请填写";
                errorContent += "[问题"+bean.getForder()+"]";
                errorContent += "的依据; ";

                errorTitle = bean.getFzjmc();
            }

            if (StringUtils.isNotBlank(errorTitle)){
                resultError += StringUtils.isNotBlank(resultError) ? "\n" : "";
                resultError += "【" + errorTitle + "】" + errorContent;
            }
        }

        if (StringUtils.isNotBlank(resultError)){
            throw new BusinessException(resultError);
        }
    }

    /**
     * 构建现场检查章节
     * @param zjml 章节
     * @param sqsxid 申请事项id
     * @param fqyid 企业id
     * @param order 排序
     * @return
     */
    private SpcyFgXcjczj generateXcjcZj(SpcyFgZjml zjml, String sqsxid, String fqyid, int order, String ffgid, String txfzrid){
        SpcyFgXcjczj xcjczj = new SpcyFgXcjczj();
        xcjczj.setId(GetUuIdUtils.ReplaceUuId());
        xcjczj.setForder(order);
        xcjczj.setFfgid(ffgid);
        xcjczj.setFzjid(zjml.getId());
        xcjczj.setFzjmc(zjml.getFzjmc());
        xcjczj.setFsqsxid(sqsxid);
        xcjczj.setFqyid(fqyid);
        xcjczj.setFtxfzrid(txfzrid);
        xcjczj.setFwczt(SpcyCommonConstant.FLFG_XCJCZJ_WCZT_WWC);
        xcjczj.setFdycs(0);

        return xcjczj;
    }

    // 根据申请事项-获取对应的现场检查章节默认法规id
    public String queryXcjczjDefaultFgBySqsx(SpcySqsxBasic sqsx){
        if (sqsx != null && StringUtils.isNotBlank(sqsx.getFywflcode())){

            /** 药品生产-第一个 */
            String fzdwjid = spcyRsYwzdxxService.querySqsxZdywId(sqsx.getFywflcode());
            List<SpcyFgFgmc> fgList = this.queryFgListByZdwjId(fzdwjid);
            if (CollectionUtil.isNotEmpty(fgList)){
                return fgList.get(0).getId();
            }else {
                return "";
            }

            /*if (sqsx.getFywflcode().equals(SpcyConstant.YWFL_CODE_YPSC)){
//                fzdwjid = SpcyCommonConstant.YPSC_ZDWJ_ID;
                List<SpcyFgFgmc> fgList = this.queryFgListByZdwjId(fzdwjid);
                if (CollectionUtil.isNotEmpty(fgList)){
                    return fgList.get(0).getId();
                }
            }else {
                return "";
            }*/
        }

        return "";
    }

    /**
     * 获取现场检查章节-根据申请事项id
     * @param sqsxid
     * @return
     */
    public List<SpcyFgXcjczj> queryXcjcZjBySqsxId(String sqsxid){
        return spcyFgXcjczjService.list(new LambdaQueryWrapper<SpcyFgXcjczj>()
                .eq(SpcyFgXcjczj::getFsqsxid, sqsxid)
                .orderByAsc(SpcyFgXcjczj::getForder));
    }

    /**
     * 发现缺陷情况选择对应的现场检查项目列表
     */
    public List<FgZjListVo> quertJczjNameListBySqsxId(String sqsxid){
        return spcyFgZjmlMapper.selectJczjNameListBySqsxId(sqsxid);
    }

    /**
     * 获取现场检查章节-根据申请事项id
     * @param sqsxid
     * @return
     */
    public List<SpcyFgXcjczj> queryZzXcjcZjBySqsxId(String sqsxid, String ftxfzrid){

        return spcyFgXcjczjService.list(new LambdaQueryWrapper<SpcyFgXcjczj>()
                .eq(SpcyFgXcjczj::getFsqsxid, sqsxid)
                .and(f->f.like(SpcyFgXcjczj::getFtxfzrid, ftxfzrid).or().isNull(SpcyFgXcjczj::getFtxfzrid))
                .orderByAsc(SpcyFgXcjczj::getForder));
    }

    /**
     * 获取现场检查章节-根据申请事项id和填写负责人id
     * @param sqsxid
     * @return
     */
    public List<SpcyFgXcjczj> queryXcjcZjBySqsxId(String sqsxid, String ftxfzrid){
        return spcyFgXcjczjService.list(new LambdaQueryWrapper<SpcyFgXcjczj>()
                .eq(SpcyFgXcjczj::getFsqsxid, sqsxid)
                .eq(SpcyFgXcjczj::getFtxfzrid, ftxfzrid)
                .orderByAsc(SpcyFgXcjczj::getForder));

    }

    /**
     * 获取用户负责的现场检查章节
     * @param sqsxid
     * @param isZz true: 组长
     * @return
     * {
     *      zjList: 章节列表
     *      hjSize: 合计数量
     *      wcSize: 完成数量
     *      wwcSize: 未完成数量
     * }
     */
    public Map queryXcjczjByNowUser(String sqsxid, boolean isZz){

        /** 如果当前为组长查询所有章节、其他只查询个人负责的章节 */
        List<SpcyFgXcjczj> xcjczjList = this.queryXcjcZjBySqsxId(sqsxid);
        /*if (isZz) {
            xcjczjList = this.queryXcjcZjBySqsxId(sqsxid);
        }else {
            xcjczjList = this.queryXcjcZjBySqsxId(sqsxid, GetCurrentUserUtils.getCurrentUserId());
        }*/

        Map resultMap = new HashMap();
        resultMap.put("zjList", xcjczjList);
        resultMap.put("hjSize", xcjczjList.size()); // 合计数量

        int wcSize = 0; // 完成数量
        int wwcSize = 0; // 未完成数量

        if (CollectionUtil.isNotEmpty(xcjczjList)){
            List<SpcyFgXcjczj> wcList = xcjczjList.stream()
                    .filter(f -> StringUtils.isNotBlank(f.getFwczt()) && f.getFwczt().equals(SpcyCommonConstant.FLFG_XCJCZJ_WCZT_YWC))
                    .collect(Collectors.toList());
            wcSize = wcList.size();

            List<SpcyFgXcjczj> wwcList = xcjczjList.stream()
                    .filter(f -> StringUtils.isBlank(f.getFwczt()) || f.getFwczt().equals(SpcyCommonConstant.FLFG_XCJCZJ_WCZT_WWC))
                    .collect(Collectors.toList());
            wwcSize = wwcList.size();
        }

        resultMap.put("wcSize", wcSize);
        resultMap.put("wwcSize", wwcSize);
        return resultMap;
    }

    /**
     * 获取用户人员情况描述检查章节
     * */
    public Map getRyqkmsZjList(String sqsxid, boolean isZz){

        /** 如果当前为组长查询所有章节、其他只查询个人负责的章节 */
        List<SpcyFgXcjczj> xcjczjList = this.queryXcjcZjBySqsxId(sqsxid);
        if (isZz) {
            xcjczjList = this.queryXcjcZjBySqsxId(sqsxid);
        }else {
            xcjczjList = this.queryXcjcZjBySqsxId(sqsxid, GetCurrentUserUtils.getCurrentUserId());
        }

        Map resultMap = new HashMap();
        resultMap.put("zjList", xcjczjList);

        return resultMap;
    }

    /**
     * 更新用户人员情况描述检查章节
     */
    public Result updateRyqkmsZjContent(SpcyFgXcjczj paramVo){
        if (StringUtils.isBlank(paramVo.getId())){
            return Result.error("参数为空！");
        }

        LambdaUpdateWrapper<SpcyFgXcjczj> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcyFgXcjczj::getId, paramVo.getId());

        SpcyFgXcjczj updateBean = new SpcyFgXcjczj();
        updateBean.setFjcms(paramVo.getFjcms());

        spcyFgXcjczjService.update(updateBean, updateWrapper);

        SpcyFgXcjczj zjxx = spcyFgZjmlMapper.selectJczjxxById(paramVo.getId());
        if (zjxx != null) {
            spcyLogService.addLogs(zjxx.getFzjmc(), paramVo.getFjcms(), paramVo.getId() + "/" + zjxx.getFsqsxid(), "更新情况描述检查章节", SpcyConstant.LOGS_SYS_TYPE_XG);
        }else {
            spcyLogService.addLogs("更新情况描述检查章节", paramVo.getFjcms(), paramVo.getId(), "更新情况描述检查章节", SpcyConstant.LOGS_SYS_TYPE_XG);
        }

        return Result.ok();
    }

    /**
     * 更改现场检查章节的填写负责人
     * @param id 检查章节id
     * @param userid 用户id
     */
    public Result modifyXcjcZjInFzr(String id, String userid){
        if (StringUtils.isBlank(id)){
            return Result.error("章节参数为空！");
        }
        if (StringUtils.isBlank(userid)){
            return Result.error("请选择负责人！");
        }

        SpcyFgXcjczj update = spcyFgXcjczjService.getById(id);
        if (update == null){
            return Result.error("没有想匹配的章节数据！");
        }

        update.setFtxfzrid(userid);
        spcyFgXcjczjService.updateById(update);

        return Result.ok();
    }

    /**
     * 获取现场检查章节任务分配 - 添加
     * @param id 现场检查章节id
     */
    public void getXcjcRwfpPersonAdd(String id, String userid){
        LambdaUpdateWrapper<SpcyFgXcjczj> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcyFgXcjczj::getId, id);

        SpcyFgXcjczj updateBean = new SpcyFgXcjczj();
        updateBean.setFtxfzrid(userid);

        spcyFgXcjczjService.update(updateBean, updateWrapper);
    }

    /**
     * 获取现场检查章节任务分配 - 移除
     * @param id 现场检查章节id
     */
    public void getXcjcRwfpPersonRemove(String id){
        LambdaUpdateWrapper<SpcyFgXcjczj> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcyFgXcjczj::getId, id);

        SpcyFgXcjczj updateBean = new SpcyFgXcjczj();
        updateBean.setFtxfzrid("");

        spcyFgXcjczjService.update(updateBean, updateWrapper);
    }

    /**
     * 更新现场检查章节
     * @param map {id: 检查章节id}
     */
    public void updatwXcjczj(HashMap<String,Object> map){
        String id = map.get("id").toString();

        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            throw new BusinessException("当前登录已失效！请重新登录！");
        }

        SpcyFgXcjczj update = spcyFgXcjczjService.getById(id);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(update,map);
        } catch (IllegalAccessException e) {
            log.error("更新现场检查章节出错：【IllegalAccessException】-> {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("更新现场检查章节出错：【InvocationTargetException】-> {}",e.getMessage());
        }

        update.setFtjrid(currentUser.getId()+"/"+currentUser.getDeptName());
        update.setFtjsj(new Date());
        spcyFgXcjczjService.updateById(update);
    }

    /******************************************【 现场检查问题 】*****************************************************/

    /**
     * 查询现场检查发现问题
     * @param sqsxid 申请事项id
     * @param ffxryid 发现人员id
     * @return
     * [{
     *     wt: 发现问题对象
     *     tkList: 条款列表
     * }]
     */
    public List<Map> queryListXcjcwtBySqsxId(String sqsxid, String ffxryid){
        List<Map> resultMap = new ArrayList<>();

        // 检查组置空，查询全部
        if (StringUtils.isNotBlank(ffxryid) && ffxryid.equals(SpcyCommonConstant.FLFG_XCJCWT_FXRYID_JCZID)){
            ffxryid = "";
        }

        // 申请事项对应的检查章节列表
        List<SpcyFgXcjcwt> wtList = spcyFgXcjcwtService.list(new LambdaQueryWrapper<SpcyFgXcjcwt>()
                .eq(SpcyFgXcjcwt::getFsqsxid, sqsxid)
                .eq(StringUtils.isNotBlank(ffxryid), SpcyFgXcjcwt::getFfxryid, ffxryid)
                .orderByDesc(SpcyFgXcjcwt::getForder)
                .orderByDesc(SpcyFgXcjcwt::getCreateTime));

        Map wtMap = null;

        for (int n=0; n<wtList.size(); n++){
            wtMap = new HashMap();

            /*Map wtdataMap = new HashMap();
            wtdataMap.put("id", wtList.get(n).getId());
            wtdataMap.put("fwtms", wtList.get(n).getFwtms());
            wtdataMap.put("fyzcd", wtList.get(n).getFyzcd());
            wtdataMap.put("forder", wtList.get(n).getForder());
            wtdataMap.put("ffxryid", wtList.get(n).getFfxryid());
            wtdataMap.put("ffxryxm", wtList.get(n).getFfxryxm());*/

            wtMap.put("wt", wtList.get(n));

            // 检查章节对应的条款问题
            wtMap.put("tkList", this.selectXcjcwtTkList(wtList.get(n).getId()));

            resultMap.add(wtMap);
        }

        return resultMap;
    }

    /**
     * 查询发现问题-未完成
     * @param sqsxid
     * @return
     */
    public List<Map> selectWwcFxwtBySqsxId(String sqsxid){
        List<Map> resultMap = new ArrayList<>();

        // 申请事项对应的检查章节列表
        List<SpcyFgXcjcwt> wtList = spcyFgXcjcwtService.list(new LambdaQueryWrapper<SpcyFgXcjcwt>()
                .eq(SpcyFgXcjcwt::getFsqsxid, sqsxid)
                .eq(SpcyFgXcjcwt::getFcjryid, GetCurrentUserUtils.getCurrentUserId())
                .eq(SpcyFgXcjcwt::getFwtzt, SpcyCommonConstant.FLFG_XCJCWT_WCZT_WWC)
                .orderByDesc(SpcyFgXcjcwt::getForder)
                .orderByDesc(SpcyFgXcjcwt::getCreateTime));

        Map wtMap = null;

        for (int n=0; n<wtList.size(); n++){
            wtMap = new HashMap();

            wtMap.put("wt", wtList.get(n));

            // 检查章节对应的条款问题
            wtMap.put("tkList", this.selectXcjcwtTkList(wtList.get(n).getId()));

            resultMap.add(wtMap);
        }

        return resultMap;
    }

    /**
     * 查询发现问题-已完成
     * */
    public List<Map> selectYwcFxwtBySqsxId(String sqsxid, String ffxryid){
        List<Map> resultMap = new ArrayList<>();

        // 检查组置空，查询全部
        if (StringUtils.isNotBlank(ffxryid) && ffxryid.equals(SpcyCommonConstant.FLFG_XCJCWT_FXRYID_JCZID)){
            ffxryid = "";
        }

        // 申请事项对应的检查章节列表
        List<SpcyFgXcjcwt> wtList = spcyFgXcjcwtService.list(new LambdaQueryWrapper<SpcyFgXcjcwt>()
                .eq(SpcyFgXcjcwt::getFsqsxid, sqsxid)
                .like(StringUtils.isNotBlank(ffxryid), SpcyFgXcjcwt::getFfxryid, ffxryid)
                .eq(SpcyFgXcjcwt::getFwtzt, SpcyCommonConstant.FLFG_XCJCWT_WCZT_YWC)
                .orderByAsc(SpcyFgXcjcwt::getForder)
                .orderByAsc(SpcyFgXcjcwt::getCreateTime));

        Map wtMap = null;

        for (int n=0; n<wtList.size(); n++){
            wtMap = new HashMap();

            wtMap.put("wt", wtList.get(n));

            // 检查章节对应的条款问题
            wtMap.put("tkList", this.selectXcjcwtTkList(wtList.get(n).getId()));

            resultMap.add(wtMap);
        }

        return resultMap;
    }

    /** 更新问题序号 */
    public HttpResult updateWtOrder(SpcyFgXcjcwt xcjcwt){
        if (StringUtils.isBlank(xcjcwt.getId())){
            return HttpResult.error("问题参数为空！");
        }

        if (xcjcwt.getForder() == null){
            return HttpResult.error("序号不能为空！");
        }

        LambdaUpdateWrapper<SpcyFgXcjcwt> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcyFgXcjcwt::getId, xcjcwt.getId());

        SpcyFgXcjcwt updateBean = new SpcyFgXcjcwt();
        updateBean.setForder(xcjcwt.getForder());
        updateBean.setCreateTime(new Date());
        spcyFgXcjcwtService.update(updateBean, updateWrapper);

        return HttpResult.ok("保存成功");
    }

    /**
     *  查询发现问题-根据问题id
     * */
    public HttpResult selecyFxwtJtkById(String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("问题参数为空！");
        }
        SpcyFgXcjcwt wt = spcyFgXcjcwtService.getById(id);
        if (wt == null){
            return HttpResult.error("没有相匹配的问题数据！");
        }

        Map wtMap = new HashMap();
        wtMap.put("wt", wt);
        wtMap.put("tkList", this.selectXcjcwtTkList(wt.getId()));
        return HttpResult.ok(wtMap);
    }

    /** 更新发现问题-完成状态
     *
     * */
    public Result updateFxwtState(String id, boolean isYwc){
        if (StringUtils.isBlank(id)){
            return Result.error("参数为空！");
        }

        if (isYwc){
            LambdaUpdateWrapper<SpcyFgXcjcwt> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcyFgXcjcwt::getId, id);

            SpcyFgXcjcwt updateBean = new SpcyFgXcjcwt();
            updateBean.setFwtzt(SpcyCommonConstant.FLFG_XCJCWT_WCZT_YWC);
            spcyFgXcjcwtService.update(updateBean, updateWrapper);

        }else {
            LambdaUpdateWrapper<SpcyFgXcjcwt> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcyFgXcjcwt::getId, id);

            SpcyFgXcjcwt updateBean = new SpcyFgXcjcwt();
            updateBean.setFwtzt(SpcyCommonConstant.FLFG_XCJCWT_WCZT_WWC);
            spcyFgXcjcwtService.update(updateBean, updateWrapper);
        }

        return Result.ok();
    }

    /**
     * 查询当前问题-存在对应相同条款的已完成问题
     * @param id 问题id
     */
    public HttpResult selectXttkFxwtList(String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        List<SpcyFgXcjcwt> resultWtList = new ArrayList<>();
        String sqsxid = spcyFgZjmlMapper.selectFsqsxidByXcjcwtId(id);

        if (StringUtils.isNotBlank(sqsxid)) {
            List<SpcyFgXcjcwttk> tkList = spcyFgXcjcwttkService.list(new LambdaQueryWrapper<SpcyFgXcjcwttk>()
                    .eq(SpcyFgXcjcwttk::getFxcjcwtid, id)
                    .isNotNull(SpcyFgXcjcwttk::getFtkid));
            if (CollectionUtil.isNotEmpty(tkList)){
                resultWtList = spcyFgZjmlMapper.selectFxwtListByFsqsxAndTkid(sqsxid,
                        tkList.get(0).getFtkid(),
                        SpcyCommonConstant.FLFG_XCJCWT_WCZT_YWC);
            }
        }

        return HttpResultVo.HttpResultList(resultWtList);
    }

    /**
     * 合并相同问题条款的数据
     * @param nowWtid 当前问题id
     * @param hbWtids 合并问题ids
     */
    public void hbXtWttkData(String nowWtid, String hbWtids){
        /** 当前问题 */
        SpcyFgXcjcwt wt = spcyFgXcjcwtService.getById(nowWtid);

        String oldWt = Optional.ofNullable(wt.getFwtms()).orElseGet(()->"");
        String oldFxryids = Optional.ofNullable(wt.getFfxryid()).orElseGet(()->"");

        /** 当前问题条款 */
        List<SpcyFgXcjcwttk> tkList = spcyFgXcjcwttkService.list(new LambdaQueryWrapper<SpcyFgXcjcwttk>()
                .eq(SpcyFgXcjcwttk::getFxcjcwtid, nowWtid));

        /** 当前问题条款所有条款id */
        List<String> tkIdList = tkList.stream()
                .filter(f -> StringUtils.isNotBlank(f.getFtkid()))
                .map(SpcyFgXcjcwttk::getFtkid)
                .distinct()
                .collect(Collectors.toList());

        List<SpcyFgXcjcwttk> newTkList = new ArrayList<>();

        /** */
        List<String> hbwtIdsList = StringPatternUtils.getListByIds(hbWtids);
        // 被合并问题id
        List<SpcyFgXcjcwt> hbWtList = spcyFgXcjcwtService.listByIds(hbwtIdsList);
        // 被合并问题的条款
        List<SpcyFgXcjcwttk> hbTkList = spcyFgXcjcwttkService.list(new LambdaQueryWrapper<SpcyFgXcjcwttk>()
                .in(SpcyFgXcjcwttk::getFxcjcwtid, hbwtIdsList));

        int orderIndex = 0;
        for (SpcyFgXcjcwt hbWt : hbWtList){

            List<SpcyFgXcjcwttk> hbTkDataList = hbTkList.stream()
                    .filter(f->f.getFxcjcwtid().equals(hbWt.getId()))
                    .collect(Collectors.toList());

            for (SpcyFgXcjcwttk hbTk : hbTkDataList){

                if (StringUtils.isNotBlank(hbTk.getFtkid()) && tkIdList.contains(hbTk.getFtkid())){
                    continue;
                }

                SpcyFgXcjcwttk newTk = new SpcyFgXcjcwttk();
                BeanUtil.copyProperties(hbTk, newTk);

                newTk.setId(GetUuIdUtils.NotReplaceUuId());
                newTk.setFxcjcwtid(wt.getId());

                newTk.setCreateTime(new Date());

                newTkList.add(newTk);
            }

            for (String hbRyid : StringPatternUtils.getListByIds(hbWt.getFfxryid())){
                if (!oldFxryids.contains(hbRyid)){
                    oldFxryids += "," + hbRyid;
                }
            }

            if (StringUtils.isNotBlank(hbWt.getFwtms())){
                oldWt = StringUtils.isNotBlank(oldWt) ? "\n" : "";
                oldWt += hbWt.getFwtms();
            }

            orderIndex++;
        }

        wt.setFwtms(oldWt);

        String fxryxm = "";
        List<YcSysUser> xmList = ycSysUserMapper.findRealnameListByIds(StringPatternUtils.getListByIds(oldFxryids));
        for (YcSysUser user : xmList){
            fxryxm += StringUtils.isNotBlank(fxryxm) ? "," : "";
            fxryxm += user.getRealname();
        }

        wt.setFwtms(oldWt);
        wt.setFwtzt(SpcyCommonConstant.FLFG_XCJCWT_WCZT_YWC);
        wt.setFfxryid(oldFxryids);
        wt.setFfxryxm(fxryxm);
        spcyFgXcjcwtService.updateById(wt);

        if (CollectionUtil.isNotEmpty(newTkList)){
            spcyFgXcjcwttkService.saveBatch(newTkList);
        }
    }

    /** 查询问题条款 */
    public List<Map> selectXcjcwtTkList(String fxcjcwtid){

        List<Map> tkMapList = new ArrayList<>();
        List<SpcyFgXcjcwttk> tkList = this.queryListXcjcwttk(fxcjcwtid);
        if (CollectionUtil.isNotEmpty(tkList)) {
            for (SpcyFgXcjcwttk tk : tkList) {
                Map dataPar = new HashMap();
                dataPar.put("id", tk.getId());
                dataPar.put("ftkid", tk.getFtkid());
                dataPar.put("tk", tk.getFfgtk());
                tkMapList.add(dataPar);
            }
        }

        return tkMapList;
    }

    /**
     * 查询章节对应发现问题列表
     * @param fxcjczjid 检查章节id
     * @return
     */
    private List<SpcyFgXcjcwt> queryListXcjcwtByWtId(String fxcjczjid){
        List<SpcyFgXcjcwt> list = spcyFgXcjcwtService.list(new LambdaQueryWrapper<SpcyFgXcjcwt>()
                .eq(SpcyFgXcjcwt::getFxcjczjid, fxcjczjid)
                .orderByAsc(SpcyFgXcjcwt::getForder));

        return list;
    }

    /**
     * 查询章节对应发现问题列表
     * @param fxcjczjid 检查章节id
     * @return
     */
    private List<SpcyFgXcjcwt> queryListXcjcwtByWtIdDesc(String fxcjczjid){
        List<SpcyFgXcjcwt> list = spcyFgXcjcwtService.list(new LambdaQueryWrapper<SpcyFgXcjcwt>()
                .eq(SpcyFgXcjcwt::getFxcjczjid, fxcjczjid)
                .orderByDesc(SpcyFgXcjcwt::getForder));

        return list;
    }

    /**
     * 查询发现问题列表-根据申请事项id
     * @param sqsxid 申请事项id
     * @return
     */
    private List<SpcyFgXcjcwt> queryListBySqsxId(String sqsxid){
        List<SpcyFgXcjcwt> list = spcyFgXcjcwtService.list(new LambdaQueryWrapper<SpcyFgXcjcwt>()
                .eq(SpcyFgXcjcwt::getFsqsxid, sqsxid)
                .eq(SpcyFgXcjcwt::getFwtzt, SpcyCommonConstant.FLFG_XCJCWT_WCZT_YWC)
                .orderByAsc(SpcyFgXcjcwt::getForder)
                .orderByAsc(SpcyFgXcjcwt::getCreateTime));

        return list;
    }

    /**
     * 验证现场检查章节-任务分配-是否已经全部分配
     */
    public void verifyXcjczjRwfp(String sqsxid,SpcySqsxBasic sqsx, String fgid){
        if (StringUtils.isBlank(fgid)) {
            fgid = queryXcjczjDefaultFgBySqsx(sqsx);
        }
        List<SpcyFgXcjczj> list = spcyFgXcjczjService.list(new LambdaQueryWrapper<SpcyFgXcjczj>()
                .eq(SpcyFgXcjczj::getFsqsxid, sqsxid)
                .eq(SpcyFgXcjczj::getFfgid, fgid)
                .isNull(SpcyFgXcjczj::getFtxfzrid));
        if (CollectionUtil.isNotEmpty(list)){
            throw new BusinessException("请分配完所有章节对应的负责人！");
        }
    }

    /**
     * 验证现场检查章节-是否全部完成
     */
    public void verifyXcjczjWczt(String sqsxid,SpcySqsxBasic sqsx, String fgid){
        /*if (StringUtils.isBlank(fgid)) {
            fgid = queryXcjczjDefaultFgBySqsx(sqsx);
        }*/

        // 是否已经全部完成
        List<SpcyFgXcjczj> list = spcyFgXcjczjService.list(new LambdaQueryWrapper<SpcyFgXcjczj>()
                .eq(SpcyFgXcjczj::getFsqsxid, sqsxid)
                .eq(SpcyFgXcjczj::getFwczt, SpcyCommonConstant.FLFG_XCJCZJ_WCZT_WWC));
        if (CollectionUtil.isNotEmpty(list)){
            throw new BusinessException("请先完成[检查总体情况]所有章节的完成情况！");
        }
    }

    /**
     * 选择历史发现问题
     * @param lswttkid 历史发现问题条款id
     * @param fxcjczjid 现场检查章节id
     * @param sqsxid 申请事项id
     */
    public void selectHistoryXcjcwt(String lswttkid,String fxcjczjid,String sqsxid){
        if (StringUtils.isBlank(lswttkid)){
            throw new BusinessException("请选择历史问题条款!");
        }
        if (StringUtils.isBlank(fxcjczjid)){
            throw new BusinessException("请选择章节条款!");
        }

        SpcyFgXcjcwttk lsWttk = spcyFgXcjcwttkService.getById(lswttkid);
        if (lsWttk == null){
            throw new BusinessException("当前选择的历史问题条款不存在！");
        }

        SpcyFgXcjcwt wt = spcyFgXcjcwtService.getById(lsWttk.getFxcjcwtid());
        if (wt == null){
            throw new BusinessException("当前选择的历史问题不存在！");
        }

        SpcyFgXcjcwt newWt = new SpcyFgXcjcwt();
        try {
            BeanUtils.copyProperties(newWt, wt);
        } catch (IllegalAccessException e) {
            log.error("问题-选择历史发现问题出错：[IllegalAccessException]->{}", e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("问题-选择历史发现问题出错：[InvocationTargetException]->{}", e.getMessage());
        }
        newWt.setId(GetUuIdUtils.ReplaceUuId());
        newWt.setFxcjczjid(fxcjczjid);
        newWt.setFsqsxid(sqsxid);

        List<SpcyFgXcjcwt> countList = this.queryListXcjcwtByWtId(fxcjczjid);
        if (CollectionUtil.isNotEmpty(countList)){
            newWt.setForder(countList.get(0).getForder()+1);
        }else {
            newWt.setForder(countList.size()+1);
        }

        spcyFgXcjcwtService.save(newWt);

        SpcyFgXcjcwttk newWttk = new SpcyFgXcjcwttk();
        try {
            BeanUtils.copyProperties(newWttk, lsWttk);
        } catch (IllegalAccessException e) {
            log.error("问题条款-选择历史发现问题出错：[IllegalAccessException]->{}", e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("问题条款-选择历史发现问题出错：[InvocationTargetException]->{}", e.getMessage());
        }
        newWttk.setId(GetUuIdUtils.ReplaceUuId());
        newWttk.setFxcjcwtid(newWt.getId());
        newWttk.setFsqsxid(sqsxid);
        spcyFgXcjcwttkService.save(newWttk);

    }

    /**
     * 现场检查章节-完成
     */
    public Result xcjczjByWc(String id){
        if (StringUtils.isBlank(id)){
            return Result.error("章节参数为空！");
        }

        SpcyFgXcjczj beanZj = spcyFgXcjczjService.getById(id);
        if (beanZj == null){
            return Result.error("没有相匹配的章节数据！");
        }
        /*if (StringUtils.isBlank(beanZj.getFjcms())){
            return Result.error("请先填写并保存章节描述！");
        }*/

        /** 验证是否已经完成章节下的相关内容 */
        /*String resultError = this.verifyJczjIsWc(beanZj.getFsqsxid(), beanZj.getId());
        if (StringUtils.isNotBlank(resultError)){
            return Result.error(resultError);
        }*/

        beanZj.setFwczt(SpcyCommonConstant.FLFG_XCJCZJ_WCZT_YWC);
        spcyFgXcjczjService.updateById(beanZj);

        return Result.ok();
    }

    /**
     * 验证是否已经完成章节下的相关内容
     * */
    private String verifyJczjIsWc(String sqsxid, String zjid){

        String resultError = "";
        List<XcjcMsWtTkVo> wtAndTkList = spcyFgZjmlMapper.selectWtAndTkBySqsxIdAndFzjid(sqsxid, zjid);
        if (CollectionUtil.isNotEmpty(wtAndTkList)) {
            String errorContent = "";
            String errorTitle = "";
            for (XcjcMsWtTkVo bean : wtAndTkList) {
                errorTitle = "";
                errorContent = "";

                /*if (StringUtils.isBlank(bean.getFjcms())) {
                    errorContent += "请填写[章节的描述]; ";
                    errorTitle = bean.getFzjmc();
                }*/
                if (StringUtils.isBlank(bean.getFyzcd())) {
                    errorContent += "请填写";
                    errorContent += "[问题" + bean.getForder() + "]";
                    errorContent += "缺陷类型; ";

                    errorTitle = bean.getFzjmc();
                }
                if (StringUtils.isBlank(bean.getFwtms())) {
                    errorContent += "请填写";
                    errorContent += "[问题" + bean.getForder() + "]";
                    errorContent += "的描述; ";

                    errorTitle = bean.getFzjmc();
                }
                if (StringUtils.isBlank(bean.getFfgtk())) {
                    errorContent += "请填写";
                    errorContent += "[问题" + bean.getForder() + "]";
                    errorContent += "的依据; ";

                    errorTitle = bean.getFzjmc();
                }

                if (StringUtils.isNotBlank(errorTitle)) {
                    resultError += StringUtils.isNotBlank(resultError) ? "\n" : "";
                    resultError += "【" + errorTitle + "】" + errorContent;
                }
            }
        }

        return resultError;
    }

    /**
     * 现场检查章节-批量完成
     * @param isZz true: 组长
     */
    public Result xcjczjByPlwc(String sqsxid, boolean isZz){
        if (StringUtils.isBlank(sqsxid)){
            return Result.error("事项参数为空！");
        }

        /** 组长验证全部章节 */
        /*LambdaQueryWrapper<SpcyFgXcjczj> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcyFgXcjczj::getFsqsxid, sqsxid);
        if (!isZz){
            queryWrapper.like(SpcyFgXcjczj::getFtxfzrid, GetCurrentUserUtils.getCurrentUserId());
        }
        queryWrapper.isNull(SpcyFgXcjczj::getFjcms);
        queryWrapper.orderByAsc(SpcyFgXcjczj::getForder);

        List<SpcyFgXcjczj> list = spcyFgXcjczjService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)){
            String error = "请完成章节的描述！";
            for (SpcyFgXcjczj b : list){
                error += "\n" + b.getFzjmc();
            }
            return Result.error(error);
        }*/

        /** 组长验证全部章节 */
        LambdaUpdateWrapper<SpcyFgXcjczj> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcyFgXcjczj::getFsqsxid, sqsxid);
        if (!isZz){
            updateWrapper.like(SpcyFgXcjczj::getFtxfzrid, GetCurrentUserUtils.getCurrentUserId());
        }

        SpcyFgXcjczj updateBean = new SpcyFgXcjczj();
        updateBean.setFwczt(SpcyCommonConstant.FLFG_XCJCZJ_WCZT_YWC);
        spcyFgXcjczjService.update(updateBean, updateWrapper);

        return Result.ok();
    }

    /**
     * 获取现场检查章节数据-根据章节id
     */
    public HttpResult hqxczjById(String id){

        return HttpResult.ok(spcyFgXcjczjService.getById(id));
    }

    /**
     * 严重现场检查章节-是否已全部完成
     * @param isZz true: 组长
     * */
    public HttpResult verifyXcjczjWc(String sqsxid, boolean isZz){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }

        /** 组长验证全部章节 */
        /*LambdaQueryWrapper<SpcyFgXcjczj> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcyFgXcjczj::getFsqsxid, sqsxid);
        if (!isZz){
            queryWrapper.like(SpcyFgXcjczj::getFtxfzrid, GetCurrentUserUtils.getCurrentUserId());
        }
        queryWrapper.isNull(SpcyFgXcjczj::getFjcms);
        queryWrapper.orderByAsc(SpcyFgXcjczj::getForder);

        List<SpcyFgXcjczj> list = spcyFgXcjczjService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)){
            return HttpResult.ok(list.get(0));
        }*/

        /** 验证是否已经完成章节下的相关内容 */
        /*String resultError = this.verifyJczjIsWc(beanZj.getFsqsxid(), beanZj.getId());
        if (StringUtils.isNotBlank(resultError)){
            return HttpResult.error(resultError);
        }*/

        return HttpResult.ok();
    }

    /**
     * 现场检查章节-重新编辑
     */
    public Result xcjczjcxbj(String id){
        if (StringUtils.isBlank(id)){
            return Result.error("章节参数为空！");
        }

        LambdaUpdateWrapper<SpcyFgXcjczj> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcyFgXcjczj::getId, id);

        SpcyFgXcjczj updateBean = new SpcyFgXcjczj();
        updateBean.setFwczt(SpcyCommonConstant.FLFG_XCJCZJ_WCZT_WWC);
        spcyFgXcjczjService.update(updateBean, updateWrapper);

        return Result.ok();
    }

    /**
     * 添加现场检查问题
     * @param sqsxid 申请事项id
     * // @param fxcjczjid 检查章节id
     */
    public Result addXcjcWt(String sqsxid, String ffxryid){
        /*if (StringUtils.isBlank(fxcjczjid)){
            return Result.error("请选择章节！");
        }*/
        if (StringUtils.isBlank(sqsxid)){
            return Result.error("事项参数为空！");
        }
        /*sqsxid = spcyFgZjmlMapper.selectFsqsxidByXcjczjId(fxcjczjid);
        if (StringUtils.isBlank(sqsxid)){
            return Result.error("没有相关的章节数据！");
        }*/

        SpcyFgXcjcwt xcjcwt = new SpcyFgXcjcwt();
        xcjcwt.setId(GetUuIdUtils.ReplaceUuId());
        xcjcwt.setFsqsxid(sqsxid);
        if (StringUtils.isBlank(ffxryid)){
            ffxryid = GetCurrentUserUtils.getCurrentUserId();
        }
        xcjcwt.setFfxryid(ffxryid);
        xcjcwt.setFcjryid(GetCurrentUserUtils.getCurrentUserId());
        xcjcwt.setFwtzt(SpcyCommonConstant.FLFG_XCJCWT_WCZT_WWC);

        if (StringUtils.isNotBlank(ffxryid)){
            if (ffxryid.equals(SpcyCommonConstant.FLFG_XCJCWT_FXRYID_JCZID)){
                xcjcwt.setFfxryxm(SpcyCommonConstant.FLFG_XCJCWT_FXRYID_JCZXM);
            }else {
//                xcjcwt.setFfxryxm(ycSysUserMapper.findRealnameById(ffxryid));
                String fxryxm = "";
                List<YcSysUser> xmList = ycSysUserMapper.findRealnameListByIds(StringPatternUtils.getListByIds(xcjcwt.getFfxryid()));
                for (YcSysUser user : xmList){
                    fxryxm += StringUtils.isNotBlank(fxryxm) ? "," : "";
                    fxryxm += user.getRealname();
                }
                xcjcwt.setFfxryxm(fxryxm);
            }
        }

        Integer order = spcyFgZjmlMapper.selectFxwtCountByFsqsxId(xcjcwt.getFsqsxid());
        xcjcwt.setForder(order+1);

        xcjcwt.setFyzcd(SpcyCommonConstant.FLFG_XCJCWT_YZCD_YBQX);

        spcyFgXcjcwtService.save(xcjcwt);


        return Result.ok();
    }

    /**
     * 添加问题及条款
     * */
    @Transactional(rollbackFor = Exception.class)
    public HttpResult addXcjcwtAndTk(FgWtVo fgWtVo){
        SpcyFgXcjcwt xcjcwt = new SpcyFgXcjcwt();
        xcjcwt.setId(GetUuIdUtils.ReplaceUuId());
        xcjcwt.setFsqsxid(fgWtVo.getSqsxid());
        xcjcwt.setFfxryid(fgWtVo.getFfxryid());
        xcjcwt.setFcjryid(GetCurrentUserUtils.getCurrentUserId());

        if (fgWtVo.getSubmitState() == 0) {
            xcjcwt.setFwtzt(SpcyCommonConstant.FLFG_XCJCWT_WCZT_WWC);
        }else {
            xcjcwt.setFwtzt(SpcyCommonConstant.FLFG_XCJCWT_WCZT_YWC);
        }

        if (StringUtils.isNotBlank(fgWtVo.getFfxryid())){
            if (fgWtVo.getFfxryid().equals(SpcyCommonConstant.FLFG_XCJCWT_FXRYID_JCZID)){
                xcjcwt.setFfxryxm(SpcyCommonConstant.FLFG_XCJCWT_FXRYID_JCZXM);
            }else {
                xcjcwt.setFfxryxm(ycSysUserMapper.findRealnameById(fgWtVo.getFfxryid()));
            }
        }

        if (StringUtils.isNotBlank(fgWtVo.getFxcjczjid())){
            xcjcwt.setFxcjczjmc(spcyFgZjmlMapper.selectZjMcById(fgWtVo.getFxcjczjid()));
        }

        Integer order = spcyFgZjmlMapper.selectFxwtCountByFsqsxId(xcjcwt.getFsqsxid());
        xcjcwt.setForder(order+1);
        xcjcwt.setFyzcd(fgWtVo.getFyzcd());
        xcjcwt.setFwtms(fgWtVo.getFwtms());

        List<SpcyFgXcjcwttk> wttkList = genWttkList(fgWtVo.getTkList(), xcjcwt.getFsqsxid(), xcjcwt.getId());
        if (CollectionUtil.isNotEmpty(wttkList)){
            spcyFgXcjcwttkService.saveBatch(wttkList);
        }
        spcyFgXcjcwtService.save(xcjcwt);

        if (fgWtVo.getSubmitState() == 0) {
            return HttpResultVo.HttpResultSave(xcjcwt, "保存成功");
        }else {
            return HttpResultVo.HttpResultSave(xcjcwt, "提交成功");
        }
    }

    /**
     * 更新现场检查问题
     * @param map
     */
    public void updatwXcjcwt(HashMap<String,Object> map){
        String id = map.get("id").toString();

        SpcyFgXcjcwt update = spcyFgXcjcwtService.getById(id);
        String oldFxryid = update.getFfxryid();
        String oldJczjId = update.getFxcjczjid();

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        try {
            BeanUtils.populate(update, map);
        } catch (IllegalAccessException e) {
            log.error("更新现场检查问题出错：【IllegalAccessException】-> {}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("更新现场检查问题出错：【InvocationTargetException】-> {}",e.getMessage());
        }

        if (StringUtils.isNotBlank(update.getFfxryid())){
            if (update.getFfxryid().equals(SpcyCommonConstant.FLFG_XCJCWT_FXRYID_JCZID)){
                update.setFfxryxm(SpcyCommonConstant.FLFG_XCJCWT_FXRYID_JCZXM);
            }else {
                if (StringUtils.isBlank(oldFxryid)
                        || StringUtils.isBlank(update.getFfxryxm())
                        || !oldFxryid.equals(update.getFfxryid())) {
//                    update.setFfxryxm(ycSysUserMapper.findRealnameById(update.getFfxryid()));

                    String fxryxm = "";
                    List<YcSysUser> xmList = ycSysUserMapper.findRealnameListByIds(StringPatternUtils.getListByIds(update.getFfxryid()));
                    for (YcSysUser user : xmList){
                        fxryxm += StringUtils.isNotBlank(fxryxm) ? "," : "";
                        fxryxm += user.getRealname();
                    }
                    update.setFfxryxm(fxryxm);
                }
            }
        }

        if (StringUtils.isNotBlank(update.getFxcjczjid())){
            if (StringUtils.isBlank(oldJczjId) || !update.getFxcjczjid().equals(oldJczjId)){
                update.setFxcjczjmc(spcyFgZjmlMapper.selectZjMcById(update.getFxcjczjid()));
            }
        }

        spcyFgXcjcwtService.updateById(update);
    }

    /**
     * 删除现场检查问题
     * @param id 发现问题id
     */
    public Result deleteXcjcwt(String id){
        SpcyFgXcjcwt wt = spcyFgXcjcwtService.getById(id);
        if (wt == null){
            return Result.error("没有相匹配的问题！");
        }

        spcyFgXcjcwtService.removeById(id);

        spcyFgXcjcwttkService.remove(new LambdaQueryWrapper<SpcyFgXcjcwttk>()
                .eq(SpcyFgXcjcwttk::getFxcjcwtid, id));

        /** 对应事项的问题进行重新排序 */
        spcyFgZjmlMapper.updateXcjcwtOrderBySqsxId(wt.getFsqsxid());


        String operContent = "删除现场检查问题：";
        operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(wt));

        spcyLogService.addLogs(LogConstant.LOGS_SQSX_FG_XCJC, operContent, wt.getFsqsxid(),"删除现场检查问题", SpcyConstant.LOGS_SYS_TYPE_SC);

        return Result.ok();
    }

    /******************************************【 现场检查问题条款 】*****************************************************/

    /**
     * 查询现场检查章节对应条款列表
     * @param wtid 发现问题id
     * @return
     */
    private List<SpcyFgXcjcwttk> queryListXcjcwttk(String wtid){
        List<SpcyFgXcjcwttk> list = spcyFgXcjcwttkService.list(new LambdaQueryWrapper<SpcyFgXcjcwttk>()
                .eq(SpcyFgXcjcwttk::getFxcjcwtid, wtid)
                .orderByAsc(SpcyFgXcjcwttk::getCreateTime));

        return list;
    }

    /**
     * 添加现场检查问题条款
     * @param sqsxid 申请事项id
     * @param fxcjcwtid 发现问题id
     * @param ftkid 条款id
     * @param fzdyzid 指导原则id
     * @param isSelectTk 是否为选择条款(true: 是)
     */
    public Result addXcjcwttk(String sqsxid, String fxcjcwtid, String ftkid, String fzdyzid, boolean isSelectTk){
        if (StringUtils.isBlank(fxcjcwtid)){
            return Result.error("请选择对应的发现问题！");
        }

        if (isSelectTk && StringUtils.isBlank(ftkid)){
            return Result.error("请选择对应的条款！");
        }

        sqsxid = spcyFgZjmlMapper.selectFsqsxidByXcjcwtId(fxcjcwtid);
        if (StringUtils.isBlank(sqsxid)){
            return Result.error("没有相关的问题数据！");
        }


        SpcyFgXcjcwttk xcjcwttk = new SpcyFgXcjcwttk();
        xcjcwttk.setId(GetUuIdUtils.ReplaceUuId());
        xcjcwttk.setFsqsxid(sqsxid);
        xcjcwttk.setFxcjcwtid(fxcjcwtid);

        if (isSelectTk && StringUtils.isNotBlank(ftkid)) {
            List<SpcyFgXcjcwttk> isExistTkList = spcyFgXcjcwttkService.list(new LambdaQueryWrapper<SpcyFgXcjcwttk>()
                    .eq(SpcyFgXcjcwttk::getFxcjcwtid, fxcjcwtid)
                    .eq(SpcyFgXcjcwttk::getFtkid, ftkid));
            if (CollectionUtil.isNotEmpty(isExistTkList)){
                return Result.error("当前条款已经存在！无需再次选择此条款！");
            }

            xcjcwttk.setFtkid(ftkid);

            // 对应条款
            SpcyFgTk tk = this.queryTkById(ftkid);
            if (tk == null) {
                return Result.error("当前选择的条款不存在！请重新选择！");
            }

            xcjcwttk.setFtkid(tk.getId());
            xcjcwttk.setFtkh(tk.getFzdyzh());

            String bz = "";
            bz = this.splitFgOrZj(tk.getFfgid(), tk.getFzjid(), bz);

            bz += StringUtils.isNotBlank(bz) ? " " : "";
            bz += Optional.ofNullable(tk.getFzdyzh()).orElseGet(()->"");
            xcjcwttk.setFfgtk(bz);
        }

        if (StringUtils.isNotBlank(fzdyzid)){
            // 指导原则
            SpcyFgZdyz zdyz = this.queryZdyzListById(fzdyzid);
            if (zdyz == null){
                return Result.error("当前选择的指导原则不存在！请重新选择！");
            }

            // 指导原则对应条款
            SpcyFgTk tk = this.queryTkById(ftkid);
            if (tk != null){
                xcjcwttk.setFtkid(tk.getId());
                xcjcwttk.setFtkh(tk.getFzdyzh());
            }

            xcjcwttk.setFzdyzid(zdyz.getId());
            xcjcwttk.setFzdyzh(zdyz.getFtkh());

            String bz = StringUtils.isNotBlank(zdyz.getFtkh()) ? "指导原则 " + zdyz.getFtkh() : "";
            xcjcwttk.setFfgtk(bz);
        }

        xcjcwttk.setCreateTime(new Date());

        spcyFgXcjcwttkService.save(xcjcwttk);

        return Result.ok();
    }

    /**
     * 添加现场检查问题条款
     */
    public List<SpcyFgXcjcwttk> genWttkList(List<FgWttkVo> tkList, String sqsxid, String fxcjcwtid){
        if (CollectionUtil.isEmpty(tkList)){
            return null;
        }

        List<SpcyFgXcjcwttk> wttkList = new ArrayList<>(tkList.size());
        for (FgWttkVo fgWttkVo : tkList){
            SpcyFgXcjcwttk xcjcwttk = new SpcyFgXcjcwttk();
            xcjcwttk.setId(GetUuIdUtils.ReplaceUuId());
            xcjcwttk.setFsqsxid(sqsxid);
            xcjcwttk.setFxcjcwtid(fxcjcwtid);

            if (StringUtils.isNotBlank(fgWttkVo.getFtkid())) {
                xcjcwttk.setFtkid(fgWttkVo.getFtkid());

                // 对应条款
                SpcyFgTk tk = this.queryTkById(fgWttkVo.getFtkid());
                if (tk == null) {
                    continue;
                }

                xcjcwttk.setFtkid(tk.getId());
                xcjcwttk.setFtkh(tk.getFzdyzh());

                String bz = "";
                bz = this.splitFgOrZj(tk.getFfgid(), tk.getFzjid(), bz);

                bz += StringUtils.isNotBlank(bz) ? " " : "";
                bz += Optional.ofNullable(tk.getFzdyzh()).orElseGet(()->"");
                xcjcwttk.setFfgtk(bz);
            }else {
                if (StringUtils.isNotBlank(fgWttkVo.getFfgtk())) {
                    xcjcwttk.setFfgtk(fgWttkVo.getFfgtk());
                }
            }

            xcjcwttk.setCreateTime(new Date());

            wttkList.add(xcjcwttk);
        }

        return wttkList;
    }

    /**
     * 手动添加现场检查问题条款
     */
    public Result handAddXcjcWttk(HandXcjcwtTkVo paramVo){
        if (StringUtils.isBlank(paramVo.getFxcjcwtid())){
            return Result.error("请选择对应的发现问题！");
        }

        if (StringUtils.isBlank(paramVo.getFfgtk())){
            return Result.error("请填写条款！");
        }

        paramVo.setSqsxid(spcyFgZjmlMapper.selectFsqsxidByXcjcwtId(paramVo.getFxcjcwtid()));
        if (StringUtils.isBlank(paramVo.getSqsxid())){
            return Result.error("没有相关的问题数据！");
        }


        SpcyFgXcjcwttk xcjcwttk = new SpcyFgXcjcwttk();
        xcjcwttk.setId(GetUuIdUtils.NotReplaceUuId());
        xcjcwttk.setFsqsxid(paramVo.getSqsxid());
        xcjcwttk.setFxcjcwtid(paramVo.getFxcjcwtid());
        xcjcwttk.setFfgtk(paramVo.getFfgtk());
        spcyFgXcjcwttkService.save(xcjcwttk);

        return Result.ok();
    }

    /**
     * 修改现场检查问题条款
     */
    public Result updateHandXcjcWttk(HandXcjcwtTkVo paramVo){
        if (StringUtils.isBlank(paramVo.getId())){
            return Result.error("参数为空！");
        }
        if (StringUtils.isBlank(paramVo.getFfgtk())){
            return Result.error("内容为空！");
        }

        LambdaUpdateWrapper<SpcyFgXcjcwttk> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcyFgXcjcwttk::getId, paramVo.getId());

        SpcyFgXcjcwttk updateBean = new SpcyFgXcjcwttk();
        updateBean.setFfgtk(paramVo.getFfgtk());
        spcyFgXcjcwttkService.update(updateBean, updateWrapper);

        return Result.ok();
    }

    /**
     * 重新选择发现问题条款
     * @param id 问题条款id
     * @param fzdyzid 指导原则id
     * @param ftkid 选择条款id
     * @param isSelectTk 是否为选择条款(true: 是)
     */
    public Result againXcjcwttk(String id, String ftkid, String fzdyzid, boolean isSelectTk){
        if (StringUtils.isBlank(id)){
            return Result.error("参数为空！");
        }

        SpcyFgXcjcwttk update = spcyFgXcjcwttkService.getById(id);
        if (update == null) {
            return Result.error("没有对应匹配的问题条款！");
        }

        if (isSelectTk && StringUtils.isBlank(ftkid)){
            return Result.error("请选择对应的条款！");
        }

        if (isSelectTk && StringUtils.isNotBlank(ftkid)) {
            // 选择的条款是否为当前此条款，不是需要校验
            if (StringUtils.isNotBlank(update.getFtkid()) && !update.getFtkid().equals(ftkid)) {
                List<SpcyFgXcjcwttk> isExistTkList = spcyFgXcjcwttkService.list(new LambdaQueryWrapper<SpcyFgXcjcwttk>()
                        .eq(SpcyFgXcjcwttk::getFxcjcwtid, update.getFxcjcwtid())
                        .eq(SpcyFgXcjcwttk::getFtkid, ftkid));
                if (CollectionUtil.isNotEmpty(isExistTkList)) {
                    return Result.error("当前条款已经存在！无需再次选择此条款！");
                }
            }

            update.setFtkid(ftkid);

            // 对应条款
            SpcyFgTk tk = this.queryTkById(ftkid);
            if (tk == null) {
                return Result.error("当前选择的条款不存在！请重新选择！");
            }
            update.setFtkid(tk.getId());
            update.setFtkh(tk.getFzdyzh());

            String bz = "";
            bz = this.splitFgOrZj(tk.getFfgid(), tk.getFzjid(), bz);
            bz += StringUtils.isNotBlank(bz) ? " " : "";
            bz += Optional.ofNullable(tk.getFzdyzh()).orElseGet(()->"");
            update.setFfgtk(bz);

           /*String defaultfgtkBz = Optional.ofNullable(tk.getFzdyzh()).orElseGet(()->"");
            if (StringUtils.isBlank(tk.getFfgid())){
                update.setFfgtk("GMP "+defaultfgtkBz);
            }else {
                *//*if (fg != null){
                    String bz = StringUtils.isNotBlank(fg.getFfgjc()) ? fg.getFfgjc()+" "+defaultfgtkBz : "GMP "+defaultfgtkBz;
                    update.setFfgtk(bz);
                }else {
                    update.setFfgtk("GMP "+defaultfgtkBz);
                }*//*
            }*/

        }

        if (StringUtils.isNotBlank(fzdyzid)){
            // 指导原则
            SpcyFgZdyz zdyz = this.queryZdyzListById(fzdyzid);
            if (zdyz == null){
                return Result.error("当前选择的指导原则不存在！请重新选择！");
            }

            // 指导原则对应条款
            SpcyFgTk tk = this.queryTkById(ftkid);
            if (tk != null){
                update.setFtkid(tk.getId());
                update.setFtkh(tk.getFzdyzh());
            }

            update.setFzdyzid(zdyz.getId());
            update.setFzdyzh(zdyz.getFtkh());

            String bz = StringUtils.isNotBlank(zdyz.getFtkh()) ? "指导原则 " + zdyz.getFtkh() : "";
            update.setFfgtk(bz);
        }

        spcyFgXcjcwttkService.updateById(update);

        return Result.ok();
    }

    /** 分割法规or章节*/
    private String splitFgOrZj(String fgid, String zjid, String bz){
        /** 法规 */
        if (StringUtils.isNotBlank(fgid)) {
            SpcyFgFgmc fg = this.queryFgById(fgid);
            if (fg != null) {
                if (StringUtils.isNotBlank(fg.getFfgjc())){
                    bz += fg.getFfgjc();

                }else if (StringUtils.isNotBlank(fg.getFfgmc())){
                    String[] split = fg.getFfgmc().split(":|：");
                    if (split.length > 1) {
                        bz += split[1];
                    } else {
                        bz += split[0];
                    }
                }
//                String[] split = fg.getFfgmc().split(":|：");
//                bz += split[0];
                /*if (split.length > 1) {
                    bz += split[1];
                } else {
                    bz += split[0];
                }*/
            }
        }

        /** 章节 */
        /*if (StringUtils.isNotBlank(zjid)){
            SpcyFgXcjczj zj = spcyFgXcjczjService.getById(zjid);
            if (zj != null && StringUtils.isNotBlank(zj.getFzjmc())) {
                bz += StringUtils.isNotBlank(bz) ? " " : "";
                bz += zj.getFzjmc();
            }
        }*/

        return bz;
    }

    /**
     * 删除现场检查问题条款
     * @param id
     */
    public void deleteXcjcwttk(String id){
        spcyFgXcjcwttkService.removeById(id);
    }

    /**
     * 查询企业历史章节描述模板
     * @param sqsxid 申请事项id
     * @param fxcjczjid 现场检查章节id
     * @return
     * [{
     *     msmb: 描述模板内容
     * }]
     */
    public HttpResult getQyHistoryMsmbList(Page<Map> page,SpcySqsxBasic sqsx, String sqsxid, String fxcjczjid, String fmbnr){
        if (sqsx == null){
            log.error("查询企业历史章节描述模板出错：申请事项不存在，申请事项id【{}】", sqsxid);
            return HttpResult.ok();
        }

        if (StringUtils.isBlank(fxcjczjid)){
            throw new BusinessException("请选择章节！");
        }

        return this.getAllHistoryMsmbList(page,fmbnr,sqsx.getFqyid(), fxcjczjid,false,false);
    }

    /**
     * 所有历史章节描述模板
     * @param fxcjczjid 现场检查章节id
     * @param fqyid 企业id
     * @param isSearchMy 查询个人历史
     * @param isSearchQt 查询其他人历史
     * @return
     * [{
     *   msmb: 描述模板内容
     * }]
     */
    public HttpResult getAllHistoryMsmbList(Page<Map> page,String fmbnr, String fqyid, String fxcjczjid, boolean isSearchMy, boolean isSearchQt){

        if (StringUtils.isBlank(fxcjczjid)){
            throw new BusinessException("请选择章节！");
        }

        SpcyFgXcjczj zj = spcyFgXcjczjService.getById(fxcjczjid);
        if (zj == null){
            throw new BusinessException("当前章节不存在！");
        }

        /** 历史对应章节的检查描述 */
        /*LambdaQueryWrapper<SpcyFgXcjczj> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(fqyid), SpcyFgXcjczj::getFqyid, fqyid);
        queryWrapper.eq(StringUtils.isNotBlank(zj.getFzjid()), SpcyFgXcjczj::getFzjid, zj.getFzjid());

        // 查询个人
        if (isSearchMy){
            queryWrapper.like(SpcyFgXcjczj::getFtjrid, GetCurrentUserUtils.getCurrentUserId());
        }

        // 查询其他人
        if (isSearchQt){
            queryWrapper.notLike(SpcyFgXcjczj::getFtjrid, GetCurrentUserUtils.getCurrentUserId());
        }

        if (StringUtils.isNotBlank(fmbnr)){
            queryWrapper.like(SpcyFgXcjczj::getFjcms, fmbnr);
        }

        queryWrapper.orderByAsc(SpcyFgXcjczj::getCreateTime);
        List<SpcyFgXcjczj> zjList = spcyFgXcjczjService.list(queryWrapper);*/

        IPage<Map> pageList = spcyFgZjmlMapper.getAllHistoryMsmbList(page, fmbnr, fqyid, zj.getFzjid(), GetCurrentUserUtils.getCurrentUserId(), isSearchMy, isSearchQt);

        List<Map> zjList = pageList.getRecords();

        /** 构建返回描述模板数据结构 */
        List<Map> resultMapList = new ArrayList<>();
        Map dataMap = null;
        for (int n=0; n<zjList.size(); n++){
            dataMap = new HashMap();
            try {
                if (zjList.get(n).get(SpcyCommonConstant.XCJZZJ_ZD_FJCMS) != null) {
                    dataMap.put("fmbnr", MapUtils.ClobToString((Clob) zjList.get(n).get(SpcyCommonConstant.XCJZZJ_ZD_FJCMS)));
                }else {
                    dataMap.put("fmbnr", "");
                }
            } catch (SQLException e) {
                log.error("大文本转字符串出错：[SQLException]:->{}",e.getMessage());
            } catch (IOException e) {
                log.error("大文本转字符串出错：[IOException]:->{}",e.getMessage());
            }
            dataMap.put("fdycs", zjList.get(0).get(SpcyCommonConstant.XCJZZJ_ZD_FDYCS));
            dataMap.put("id", zjList.get(0).get(SpcyCommonConstant.XCJZZJ_ZD_ID));
            resultMapList.add(dataMap);
        }
        pageList.setRecords(resultMapList);

//        return resultMapList;
        return HttpResult.ok(pageList);
    }

    /**
     * 调用历史章节模式模板
     */
    public void selectHistoryMsmb(String id){
        log.error("调用历史章节模式模板：参数为空！");
        SpcyFgXcjczj zj = spcyFgXcjczjService.getById(id);
        if (zj == null){
            log.error("当前选择历史章节为空！参数: {}", id);
            return;
        }
        zj.setFdycs(zj.getFdycs() + 1);

        spcyFgXcjczjService.updateById(zj);
    }

    /**
     * 生成现场检查结论
     * @param sqsx 申请事项
     * @param sqsxid 申请事项id
     */
    public SqsxXcjcQxParamBo generateXcjcConclusion(SpcySqsxBasic sqsx, String sqsxid){

        if (StringUtils.isNotBlank(sqsx.getFywflcode())){
            if (sqsx.getFywflcode().equals(SpcyConstant.YWFL_CODE_YPSC)){
                return this.generateXcjcConclusionInYpsc(sqsx, sqsxid);
            }
            else if (sqsx.getFywflcode().equals(SpcyConstant.YWFL_CODE_YPZC)){
                return this.generateXcjcConclusionInYpsc(sqsx, sqsxid);
            }
            else if (sqsx.getFywflcode().equals(SpcyConstant.YWFL_CODE_YPJY)){
                return this.generateXcjcConclusionInYpsc(sqsx, sqsxid);
            }
            else if (sqsx.getFywflcode().equals(SpcyConstant.YWFL_CODE_YLQX)){
                return this.generateXcjcConclusionInYpsc(sqsx, sqsxid);
            }else if (sqsx.getFywflcode().equals(SpcyConstant.YWFL_CODE_HZP)){
                return this.generateXcjcConclusionInYpsc(sqsx, sqsxid);
            }
        }

        return new SqsxXcjcQxParamBo();
    }

    /**
     * 生成现场检查结论-药品生产
     */
    private SqsxXcjcQxParamBo generateXcjcConclusionInYpsc(SpcySqsxBasic sqsx, String sqsxid){
        String addCarriageReturn = "\n";  // 换行符
//        String addSpace = "\t\t";  // 换行符

        String org_jcyj = spcySqsxYwlxmbService.getJcyjBySqsxId(sqsx.getFywflcode());
        String fcyjcyj = Optional.ofNullable(sqsx.getFcyjcyj()).orElseGet(()->org_jcyj); // 检查依据
//        String fqymc = Optional.ofNullable(sqsx.getFqymc()).orElseGet(()->"企业"); // 企业名称
        String fjcfw = Optional.ofNullable(sqsx.getFcyjcfw()).orElseGet(()->"。。。（检查范围）"); // 检查范围
        String org_cyjg = "符合"; // 符合/基本符合/不符合

        /**
         * 替换关键字
         */
//        String replaceGjz1 = "fcyjcyj";
        String replaceGjz2 = "qxqk";
        String replaceCyjg = "cyjg";
//        String replaceGjz3 = "fqymc";

        StringBuffer temStringBuf = new StringBuffer();
        temStringBuf.append("根据").append(fcyjcyj).append("规定，检查组结合企业实际情况开展检查，");
        temStringBuf.append(replaceGjz2).append("。综合评判，该企业");
//        temStringBuf.append(fqymc);
        temStringBuf.append(fjcfw);
        temStringBuf.append(replaceCyjg);
//        temStringBuf.append("申报的xxxx符合");
        temStringBuf.append(fcyjcyj).append("的要求。");

//        String temStr = "根据{data.fcyjcyj}规定，检查组结合企业实际情况开展检查，${data.qxqk}" +
//                "综合评判，${data.fqymc}申报的xxxx符合{data.fcyjcyj}的要求。";

        /** 现场检查结论 */
        /*StringBuffer conculsion = new StringBuffer("");
        conculsion.append(addCarriageReturn);
        conculsion.append(addSpace);
        conculsion.append("现场检查发现缺陷情况如下：");*/

        /** 现场检查缺陷项目信息 */
        String fxcjcqxxm = "";
//        fxcjcqxxm += "现场检查发现缺陷情况如下：";

        int yzqxSize = 0; // 严重
        int zyqxSize = 0; // 主要
        int ybqxSize = 0; // 一般

        /** 获取缺陷项目信息 */
        SqsxXcjcQxParamBo qxParamBo = this.getJcxmQxxx(sqsxid);
        yzqxSize = qxParamBo.getYzqxSize();
        zyqxSize = qxParamBo.getZyqxSize();
        ybqxSize = qxParamBo.getYbqxSize();

        String yzqx = qxParamBo.getYzqx();
        String zyqx = qxParamBo.getZyqx();
        String ybqx = qxParamBo.getYbqx();

        /** 查验结果 */
        if (yzqxSize > 0){
            org_cyjg = "不符合";
        }else if (zyqxSize > 0){
            org_cyjg = "基本符合";
        }

        /*conculsion.append(addCarriageReturn).append(yzqx);
        conculsion.append(addCarriageReturn).append(zyqx);
        conculsion.append(addCarriageReturn).append(ybqx);*/

        fxcjcqxxm += addCarriageReturn + yzqx;
        fxcjcqxxm += addCarriageReturn + zyqx;
        fxcjcqxxm += addCarriageReturn + ybqx;


        /** 根据不同的缺陷数量生成对应的缺陷情况（描述）*/
        String qxqk = this.genarateXcjcQxqkString(yzqxSize, zyqxSize, ybqxSize);

        String replaceTemStr = temStringBuf.toString().replace(replaceCyjg, org_cyjg);
        replaceTemStr = replaceTemStr.replace(replaceGjz2, qxqk);
        /*String replaceTemStr = temStringBuf.toString().replace(replaceGjz1, fcyjcyj);
        replaceTemStr = replaceTemStr.replace(replaceGjz3, fqymc);
        replaceTemStr = replaceTemStr.replace(replaceGjz2, qxqk);*/

        String jl = replaceTemStr;
//        jl += conculsion.toString();

        qxParamBo.setJl(jl);
        qxParamBo.setFjcqxxm(fxcjcqxxm);
        qxParamBo.setFcyjg(org_cyjg);

        if (StringUtils.isBlank(sqsx.getFcyjcyj())) {
            qxParamBo.setFcyjcyj(fcyjcyj);
        }
        return qxParamBo;
    }

    /**
     * 生成综合评定审查意见
     */
    public String generateZhpdOpinion(SpcySqsxBasic sqsx, String sqsxid){

        String scyj = "";

        String jcyj = "";
        if (StringUtils.isBlank(sqsx.getFcyjcyj())){
            jcyj = spcySqsxYwlxmbService.getJcyjBySqsxId(sqsx.getFywflcode());
        }else {
            jcyj = sqsx.getFcyjcyj();
        }

//        String emptyChar = "    ";
//        scyj += emptyChar;
        scyj += "1. 检查组提交的现场检查材料等齐全，符合要求。";
        scyj += "\n";

        /** 获取缺陷项目信息 */
        SqsxXcjcQxParamBo qxParamBo = this.getJcxmQxSize(sqsxid);

        /** 缺陷情况，不带详情 */
        String qxqk = this.genarateXcjcQxqkString(qxParamBo.getYzqxSize(), qxParamBo.getZyqxSize(), qxParamBo.getYbqxSize());

//        scyj += emptyChar;
        scyj += "2. 现场检查" + qxqk + "。";
        scyj += DateConversionUtils.DateToStringNew(new Date()) + "收到企业的整改报告，";
        scyj += DateConversionUtils.DateToStringNew(new Date()) + "中心组织召开审评会，";
        scyj += "对现场检查报告进行审评，认为检查组发现的缺陷项目引用条款恰当，缺陷项目内容未隐藏有其它条款，";
        scyj += "同意检查组" + qxqk + "。";
        scyj += "\n";

//        scyj += emptyChar;
        scyj += "3. 企业提供的整改报告的整改计划已涵盖全部应整改项目，应立即整改的缺陷项目已完成整改工作。";
        scyj += "\n";

//        scyj += emptyChar;
        scyj += "4. 综合评定结果：";
        scyj += "该企业生产地址"+Optional.ofNullable(sqsx.getFxxdz()).orElseGet(()->"xx");
        scyj += "的" + Optional.ofNullable(sqsx.getFcyjcfw()).orElseGet(()->"xx");
        scyj += "符合" + jcyj + "的要求。";

        scyj += "建议" + Optional.ofNullable(sqsx.getFcygcybm()).orElseGet(()->"自治区xx检查分局") + "加强日常监管，拟移送药品生产处。";

        return scyj;
    }

    /**
     * 获取检查项目（一般缺陷、主要缺陷、严重缺陷）
     * @param sqsxid
     * @return
     * {
     *     yzqxSize、zyqxSize、ybqxSize
     *     yzqx、zyqx、ybqx
     * }
     */
    public SqsxXcjcQxParamBo getJcxmQxxx(String sqsxid){
        return getJcxmQxxx(sqsxid, true);
    }

    public SqsxXcjcQxParamBo getJcxmQxxx(String sqsxid, boolean isAddTitle){

        String addCarriageReturn = "\n";  // 换行符
//        String addSpace = "\t\t";  // 空格符
        String addSpace = "    ";  // 四个空格
        List<SpcyFgXcjcwt> wtList = this.queryListBySqsxId(sqsxid);

        int yzqxSize = 0; // 严重
        int zyqxSize = 0; // 主要
        int ybqxSize = 0; // 一般

        String ybqxTitle = "";
        String zyqxTitle = "";
        String yzqxTitle = "";
        if (isAddTitle){
            ybqxTitle = "一般缺陷: ";
            zyqxTitle = "主要缺陷: ";
            yzqxTitle = "严重缺陷: ";
        }

        StringBuffer ybqx = new StringBuffer();
        StringBuffer zyqx = new StringBuffer();
        StringBuffer yzqx = new StringBuffer();
        if (CollectionUtil.isNotEmpty(wtList)){

            /** 严重 */
            List<SpcyFgXcjcwt> yzqxList = wtList.stream()
                    .filter(f -> StringUtils.isNotBlank(f.getFyzcd()) && f.getFyzcd().equals(SpcyCommonConstant.FLFG_XCJCWT_YZCD_YZQX))
                    .collect(Collectors.toList());
            yzqxSize = yzqxList.size();
            if (yzqxSize>0){

                if (isAddTitle){
                    yzqx.append(addSpace);
                    yzqx.append(yzqxTitle);
                }

                yzqx.append(yzqxSize).append("项：");

                for (int order=0; order < yzqxList.size(); order++){
                    yzqx.append(addCarriageReturn);
                    yzqx.append(addSpace);
                    yzqx.append(order+1).append("、");
                    yzqx.append(Optional.ofNullable(yzqxList.get(order).getFwtms()).orElseGet(()->" "));

                    yzqx.append(this.getWttkContent(yzqxList.get(order).getId()));

                    /*List<SpcyFgXcjcwttk> tkList = this.queryListXcjcwttk(yzqxList.get(order).getId());
                    for (int m=0; m < tkList.size(); m++){
                        if (m == 0){
                            yzqx.append("（");
                        }else {
                            yzqx.append("、");
                        }

                        yzqx.append(Optional.ofNullable(tkList.get(m).getFfgtk()).orElseGet(()->" "));

                        if (m == (tkList.size()-1)) {
                            yzqx.append("）");
                        }
                    }*/
                }
            }else {
                yzqx.append(addSpace).append(yzqxTitle).append(yzqxSize).append("项。");
            }

            /** 主要 */
            List<SpcyFgXcjcwt> zyqxList = wtList.stream()
                    .filter(f -> StringUtils.isNotBlank(f.getFyzcd()) && f.getFyzcd().equals(SpcyCommonConstant.FLFG_XCJCWT_YZCD_ZYQX))
                    .collect(Collectors.toList());
            zyqxSize = zyqxList.size();
            if (zyqxSize>0){

                if (isAddTitle){
                    zyqx.append(addSpace);
                    zyqx.append(zyqxTitle);
                }

                zyqx.append(zyqxSize).append("项：");

                for (int order=0; order < zyqxList.size(); order++){
                    zyqx.append(addCarriageReturn);
                    zyqx.append(addSpace);
                    zyqx.append(order+1).append("、");
                    zyqx.append(Optional.ofNullable(zyqxList.get(order).getFwtms()).orElseGet(()->" "));

                    zyqx.append(this.getWttkContent(zyqxList.get(order).getId()));

                    /*List<SpcyFgXcjcwttk> tkList = this.queryListXcjcwttk(zyqxList.get(order).getId());
                    for (int m=0; m < tkList.size(); m++){

                        if (m == 0){
                            zyqx.append("（");
                        }else {
                            zyqx.append("、");
                        }

                        zyqx.append(Optional.ofNullable(tkList.get(m).getFfgtk()).orElseGet(()->" "));

                        if (m == (tkList.size()-1)) {
                            zyqx.append("）");
                        }
                    }*/
                }
            }else {
                zyqx.append(addSpace).append(zyqxTitle).append(zyqxSize).append("项。");
            }

            /** 一般 */
//            ybqz.append(addCarriageReturn);
            List<SpcyFgXcjcwt> ybqxList = wtList.stream()
                    .filter(f -> StringUtils.isNotBlank(f.getFyzcd()) && f.getFyzcd().equals(SpcyCommonConstant.FLFG_XCJCWT_YZCD_YBQX))
                    .collect(Collectors.toList());
            ybqxSize = ybqxList.size();
            if (ybqxSize>0){

                if (isAddTitle){
                    ybqx.append(addSpace);
                    ybqx.append(ybqxTitle);
                }

                ybqx.append(ybqxSize).append("项：");

                for (int order=0; order < ybqxList.size(); order++){
                    ybqx.append(addCarriageReturn);
                    ybqx.append(addSpace);
                    ybqx.append(order+1).append("、");
                    ybqx.append(Optional.ofNullable(ybqxList.get(order).getFwtms()).orElseGet(()->" "));

                    ybqx.append(this.getWttkContent(ybqxList.get(order).getId()));

                    /*List<SpcyFgXcjcwttk> tkList = this.queryListXcjcwttk(ybqxList.get(order).getId());
                    for (int m=0; m < tkList.size(); m++){

                        if (m == 0){
                            ybqx.append("（");
                        }else {
                            ybqx.append("、");
                        }

                        ybqx.append(Optional.ofNullable(tkList.get(m).getFfgtk()).orElseGet(()->" "));

                        if (m == (tkList.size()-1)) {
                            ybqx.append("）");
                        }
                    }*/
                }
            }else {
                ybqx.append(addSpace).append(ybqxTitle).append(ybqxSize).append("项。");
            }

        }

        SqsxXcjcQxParamBo paramBo = new SqsxXcjcQxParamBo();
        paramBo.setYbqxSize(ybqxSize);
        paramBo.setZyqxSize(zyqxSize);
        paramBo.setYzqxSize(yzqxSize);

        paramBo.setYzqx(yzqx.toString());
        paramBo.setZyqx(zyqx.toString());
        paramBo.setYbqx(ybqx.toString());

        return paramBo;
    }

    /** 缺陷数量 */
    public SqsxXcjcQxParamBo getJcxmQxSize(String sqsxid){
        int yzqxSize = 0; // 严重
        int zyqxSize = 0; // 主要
        int ybqxSize = 0; // 一般

        List<SpcyFgXcjcwt> wtList = this.queryListBySqsxId(sqsxid);
        if (CollectionUtil.isNotEmpty(wtList)){
            /** 严重 */
            List<SpcyFgXcjcwt> yzqxList = wtList.stream()
                    .filter(f -> StringUtils.isNotBlank(f.getFyzcd()) && f.getFyzcd().equals(SpcyCommonConstant.FLFG_XCJCWT_YZCD_YZQX))
                    .collect(Collectors.toList());
            yzqxSize = yzqxList.size();

            /** 主要 */
            List<SpcyFgXcjcwt> zyqxList = wtList.stream()
                    .filter(f -> StringUtils.isNotBlank(f.getFyzcd()) && f.getFyzcd().equals(SpcyCommonConstant.FLFG_XCJCWT_YZCD_ZYQX))
                    .collect(Collectors.toList());
            zyqxSize = zyqxList.size();

            List<SpcyFgXcjcwt> ybqxList = wtList.stream()
                    .filter(f -> StringUtils.isNotBlank(f.getFyzcd()) && f.getFyzcd().equals(SpcyCommonConstant.FLFG_XCJCWT_YZCD_YBQX))
                    .collect(Collectors.toList());
            ybqxSize = ybqxList.size();
        }

        SqsxXcjcQxParamBo paramBo = new SqsxXcjcQxParamBo();
        paramBo.setYbqxSize(ybqxSize);
        paramBo.setZyqxSize(zyqxSize);
        paramBo.setYzqxSize(yzqxSize);

        return paramBo;
    }

    /**
     * 根据不同的缺陷数量生成对应的缺陷情况（描述）
     * @param sqsxid
     * @return
     */
    private String genarateXcjcQxqkString(String sqsxid){
        int yzqxSize = 0; // 严重
        int zyqxSize = 0; // 主要
        int ybqxSize = 0; // 一般

        List<SpcyFgXcjcwt> wtList = this.queryListBySqsxId(sqsxid);
        if (CollectionUtil.isNotEmpty(wtList)){

            List<SpcyFgXcjcwt> yzqxList = wtList.stream()
                    .filter(f -> StringUtils.isNotBlank(f.getFyzcd()) && f.getFyzcd().equals(SpcyCommonConstant.FLFG_XCJCWT_YZCD_YZQX))
                    .collect(Collectors.toList());
            yzqxSize = yzqxList.size();

            List<SpcyFgXcjcwt> zyqxList = wtList.stream()
                    .filter(f -> StringUtils.isNotBlank(f.getFyzcd()) && f.getFyzcd().equals(SpcyCommonConstant.FLFG_XCJCWT_YZCD_ZYQX))
                    .collect(Collectors.toList());
            zyqxSize = zyqxList.size();

            List<SpcyFgXcjcwt> ybqxList = wtList.stream()
                    .filter(f -> StringUtils.isNotBlank(f.getFyzcd()) && f.getFyzcd().equals(SpcyCommonConstant.FLFG_XCJCWT_YZCD_YBQX))
                    .collect(Collectors.toList());
            ybqxSize = ybqxList.size();
        }

        return genarateXcjcQxqkString(yzqxSize, zyqxSize, ybqxSize);
    }

    /**
     * 根据不同的缺陷数量生成对应的缺陷情况（描述）
     * @param yzqxSize 严重缺陷数量
     * @param zyqxSize 主要缺陷数量
     * @param ybqxSize 一般缺陷数量
     * @return
     */
    private String genarateXcjcQxqkString(int yzqxSize, int zyqxSize, int ybqxSize){

        boolean yzqxExistFlag = false;
        if (yzqxSize > 0){
            yzqxExistFlag = true;
        }

        boolean zyqxExistFlag = false;
        if (zyqxSize > 0){
            zyqxExistFlag = true;
        }

        boolean ybqxExistFlag = false;
        if (ybqxSize > 0){
            ybqxExistFlag = true;
        }

        StringBuffer fxqxqk = new StringBuffer("");
        // 未发现严重缺陷
        if (!yzqxExistFlag){
            fxqxqk.append("未发现").append("严重缺陷");
        }

        // 未发现主要缺陷
        if (!zyqxExistFlag){
            // 是否存在严重缺陷
            if (fxqxqk.toString().contains("严重缺陷")){
                fxqxqk.append("和").append("主要缺陷");
            }else {
                fxqxqk.append("未发现").append("主要缺陷");
            }
        }

        // 未发现一般缺陷
        if (!ybqxExistFlag){
            // 存在严重缺陷
            if (fxqxqk.toString().contains("严重缺陷")){
                // 存在主要缺陷
                if (fxqxqk.toString().contains("主要缺陷")) {
                    fxqxqk.replace(fxqxqk.lastIndexOf("和"),fxqxqk.lastIndexOf("和")+1,"、")
                            .append("和").append("一般缺陷");
                }else {
                    fxqxqk.append("和").append("一般缺陷");
                }
            } else {  // 不存在严重缺陷
                // 存在主要缺陷
                if (fxqxqk.toString().contains("主要缺陷")) {
                    fxqxqk.append("和").append("一般缺陷");
                }else {
                    fxqxqk.append("未发现").append("一般缺陷");
                }
            }
        }

        StringBuffer fxqxqk2 = new StringBuffer("");
        // 发现严重缺陷
        if (yzqxExistFlag){
            fxqxqk2.append("发现").append("严重缺陷").append(yzqxSize).append("项");
        }

        // 发现主要缺陷
        if (zyqxExistFlag){
            if (fxqxqk2.toString().contains("严重缺陷")){
                fxqxqk2.append("，").append("发现").append("主要缺陷").append(zyqxSize).append("项");
            }else {
                fxqxqk2.append("发现").append("主要缺陷").append(zyqxSize).append("项");
            }
        }

        // 发现一般缺陷
        if (ybqxExistFlag){
            // 存在严重缺陷
            if (fxqxqk2.toString().contains("严重缺陷")) {
                // 存在主要缺陷
                if (fxqxqk2.toString().contains("主要缺陷")) {
                    fxqxqk2.append("，").append("发现").append("一般缺陷").append(ybqxSize).append("项");
                }else {
                    fxqxqk2.append("，").append("发现").append("一般缺陷").append(ybqxSize).append("项");
                }
            }else { // 不存在严重缺陷
                // 存在主要缺陷
                if (fxqxqk2.toString().contains("主要缺陷")) {
                    fxqxqk2.append("，").append("发现").append("一般缺陷").append(ybqxSize).append("项");
                }else {
                    fxqxqk2.append("发现").append("一般缺陷").append(ybqxSize).append("项");
                }
            }
        }

        String qxqk = "";
        // 没有发现有缺陷
        if (StringPatternUtils.verifryIsEmptyStringBuffer(fxqxqk)){
            qxqk = fxqxqk2.toString();
        }else if (StringPatternUtils.verifryIsEmptyStringBuffer(fxqxqk2)){
            qxqk = fxqxqk.toString();
        }else {
            qxqk = fxqxqk.toString()+"，"+fxqxqk2.toString();
        }

        return qxqk;
    }

    /**
     * 生成综合评定结论
     */
    public Map<String, String> generateZhpdConclusion(SpcySqsxBasic sqsx){
        if (sqsx == null){
            throw new BusinessException("申请事项参数为空！");
        }

        if (StringUtils.isNotBlank(sqsx.getFywflcode())){
            if (sqsx.getFywflcode().equals(SpcyConstant.YWFL_CODE_YPSC)){
                return this.generateZhpdConclusionInYpsc(sqsx);
            }
            else if (sqsx.getFywflcode().equals(SpcyConstant.YWFL_CODE_YPZC)){
                return this.generateZhpdConclusionInYpsc(sqsx);
            }
            else if (sqsx.getFywflcode().equals(SpcyConstant.YWFL_CODE_YPJY)){
                return this.generateZhpdConclusionInYpsc(sqsx);
            }
            else if (sqsx.getFywflcode().equals(SpcyConstant.YWFL_CODE_YLQX)){
                return this.generateZhpdConclusionInYpsc(sqsx);
            }else if (sqsx.getFywflcode().equals(SpcyConstant.YWFL_CODE_HZP)){
                return this.generateZhpdConclusionInYpsc(sqsx);
            }
        }

        return new HashMap<>();
    }

    /**
     * 生成综合评定结论-药品生产
     */
    private Map<String, String> generateZhpdConclusionInYpsc(SpcySqsxBasic sqsx){
//        String addCarriageReturn = "\n";  // 换行符
//        String addSpace = "\t\t";  // 换行符
        Map<String,String> resultMap = new HashMap<>();
        String jl = "";
        List<SpcyRsYwzdxx> ywzzList = spcyRsYwzdxxService.list(new LambdaQueryWrapper<SpcyRsYwzdxx>()
                .eq(SpcyRsYwzdxx::getFzdlx1, SpcyConstant.YWZDXX_ZHJL)
                .eq(SpcyRsYwzdxx::getFywflcode, sqsx.getFywflcode()));
        if (CollectionUtils.isNotEmpty(ywzzList)){
            jl += ywzzList.get(0).getFzdnr();
        }

        String zhjg = SpcyConstant.JCJG_FH;
        if (StringUtils.isNotBlank(sqsx.getFzhjg())) {
            zhjg = sqsx.getFzhjg();
        }

        String cpmc = " ";
        String gcybm = " ";
        String cyjcyj = "xxx";
        if (org.apache.commons.lang3.StringUtils.isNotBlank(sqsx.getFcpmc())){
            cpmc = sqsx.getFcpmc();
        }
        if (org.apache.commons.lang3.StringUtils.isNotBlank(sqsx.getFcygcybm())){
            gcybm = sqsx.getFcygcybm();
        }
        if (org.apache.commons.lang3.StringUtils.isNotBlank(sqsx.getFcyjcyj())){
            cyjcyj = sqsx.getFcyjcyj();
        }

        String qxqk = genarateXcjcQxqkString(sqsx.getId());

        String replace = jl.replace(SpcyConstant.YWZDXX_CPMC_REPLACE, cpmc)
                .replace(SpcyConstant.YWZDXX_ZHJG_REPLACE, zhjg)
                .replace(SpcyConstant.YWZDXX_CYGCYBM_REPLACE, gcybm)
                .replace(SpcyConstant.YWZDXX_CYJCYJ_REPLACE,cyjcyj)
                .replace(SpcyConstant.YWZDXX_FXQXQK_REPLACE, qxqk);

        resultMap.put("fzhjl",replace);
        return resultMap;
    }

    /**
     * 添加现场检查章节
     */
    public void handMovementAddXcjczj(SpcySqsxBasic sqsx, Integer forder, String fzjmc){
        SpcyFgXcjczj xcjczj = new SpcyFgXcjczj();
        xcjczj.setId(GetUuIdUtils.ReplaceUuId());
        xcjczj.setForder(forder);
        xcjczj.setFsqsxid(sqsx.getId());
        xcjczj.setFqyid(sqsx.getFqyid());
        xcjczj.setFzjmc(fzjmc);
        xcjczj.setFwczt(SpcyCommonConstant.FLFG_XCJCZJ_WCZT_WWC);
        xcjczj.setFdycs(0);
        spcyFgXcjczjService.save(xcjczj);
    }

    /**
     * 复制一条现场检查章节的情况、问题以及条款到
     * 一条新的现场检查章节
     * @param copyXcjczjId 复制现场检查章节id
     * @param targetXcjczjId 目标检查章节id
     */
    public Result copyXcjczjToOtherZj(String copyXcjczjId, String targetXcjczjId){
        if (StringUtils.isBlank(copyXcjczjId)){
            return Result.error("复制参数为空！");
        }
        if (StringUtils.isBlank(targetXcjczjId)){
            return Result.error("目标参数为空！");
        }

        SpcyFgXcjczj copyZj = spcyFgXcjczjService.getById(copyXcjczjId);
        if (copyZj == null){
            return Result.error("没有相匹配的复制章节数据！");
        }

        if (StringUtils.isBlank(copyZj.getFjcms())){
            return Result.ok();
        }

        SpcyFgXcjczj targetZj = spcyFgXcjczjService.getById(targetXcjczjId);
        if (targetZj == null){
            return Result.error("没有相匹配的目标章节数据！");
        }

        targetZj.setFjcms(copyZj.getFjcms());
        spcyFgXcjczjService.updateById(targetZj);

        List<SpcyFgXcjcwt> newTargetWtList = new ArrayList<>();
        List<SpcyFgXcjcwttk> newTargetTkList = new ArrayList<>();
        List<SpcyFgXcjcwt> targetWtList = spcyFgXcjcwtService.list(new LambdaQueryWrapper<SpcyFgXcjcwt>()
                .eq(SpcyFgXcjcwt::getFxcjczjid, targetZj.getId()));
        int targetOrder = targetWtList.size();

        List<SpcyFgXcjcwt> copyWtList = spcyFgXcjcwtService.list(new LambdaQueryWrapper<SpcyFgXcjcwt>()
                .eq(SpcyFgXcjcwt::getFxcjczjid, copyZj.getId())
                .orderByAsc(SpcyFgXcjcwt::getForder));
        for (SpcyFgXcjcwt copyWt : copyWtList){
            SpcyFgXcjcwt targetWt = new SpcyFgXcjcwt();
            BeanUtil.copyProperties(copyWt, targetWt);

            targetWt.setForder(targetOrder+1);
            targetWt.setId(GetUuIdUtils.NotReplaceUuId());
            targetWt.setFsqsxid(targetZj.getFsqsxid());

            newTargetWtList.add(targetWt);
            targetOrder++;

            List<SpcyFgXcjcwttk> copyTkList = spcyFgXcjcwttkService.list(new LambdaQueryWrapper<SpcyFgXcjcwttk>()
                    .eq(SpcyFgXcjcwttk::getFxcjcwtid, copyWt.getId()));
            for (SpcyFgXcjcwttk copyTk : copyTkList){
                SpcyFgXcjcwttk targetTk = new SpcyFgXcjcwttk();
                BeanUtil.copyProperties(copyTk, targetWt);

                targetTk.setId(GetUuIdUtils.NotReplaceUuId());
                targetTk.setFxcjcwtid(targetWt.getId());
                targetTk.setFsqsxid(targetZj.getFsqsxid());

                newTargetTkList.add(targetTk);
            }
        }

        if (CollectionUtil.isNotEmpty(newTargetWtList)) {
            spcyFgXcjcwtService.saveBatch(newTargetWtList);
        }
        if (CollectionUtil.isNotEmpty(newTargetTkList)) {
            spcyFgXcjcwttkService.saveBatch(newTargetTkList);
        }

        return Result.ok();
    }

    /**
     * 获取法律法规-法规+章节树形结构
     */
    public HttpResult getZjTree(String fzdwjid, String gjz){
        List<FlfgZjmlTreeVo> treeVoList = new ArrayList<>();
        if (StringUtils.isBlank(fzdwjid)){
            return HttpResult.ok(treeVoList);
        }

        /** 法规 */
        List<SpcyFgFgmc> fgList = spcyFgFgmcService.list(new LambdaQueryWrapper<SpcyFgFgmc>()
                .eq(SpcyFgFgmc::getFzdwjid, fzdwjid)
                .orderByAsc(SpcyFgFgmc::getForder));

        for (SpcyFgFgmc fg : fgList) {
            FlfgZjmlTreeVo treeVo = new FlfgZjmlTreeVo();

            treeVo.setId(fg.getId());
            treeVo.setKey(fg.getId());

            treeVo.setFzjmc(fg.getFfgmc());
            treeVo.setFzjjc(fg.getFfgmc());
            treeVo.setTitle(fg.getFfgmc());

            treeVo.setForder(fg.getForder());


            // 章节树形列表
            List<FlfgZjmlTreeVo> zjList = this.initTkTree(fg.getId(), fg.getId(), gjz);
            if (CollectionUtil.isEmpty(zjList) && StringUtils.isNotBlank(gjz) && !treeVo.getTitle().contains(gjz)){
                continue;
            }
            treeVo.setChildren(zjList);

            treeVoList.add(treeVo);
        }

        return HttpResult.ok(treeVoList);
    }

    /**
     * 组织现场检查章节名称及描述内容
     */
    public Map<String, String> genXcjczjStructureContent(String sqsxid){
        Map<String, String> resultMap = new HashMap<>();
        String jczj = "";

        // 获取对应模板的章节
        List<SpcyFgXcjczj> zjmlList = spcyFgXcjczjService.list(new LambdaQueryWrapper<SpcyFgXcjczj>()
                .eq(SpcyFgXcjczj::getFsqsxid, sqsxid)
                .orderByAsc(SpcyFgXcjczj::getForder));
        if (CollectionUtil.isEmpty(zjmlList)){
            resultMap.put("${data.jczj}", jczj);

            return resultMap;
        }

//        String returnCharFlag = "&lt;br&gt;";
//        String returnCharFlag = "<p><br/>";
        String returnCharFlag = "";
//        String returnCharFlag = "\n";

        /**
         *   （一）质量管理
         *    该公司建立了质量管理体系，制定了质量方针和质量目标，明确了各部门及关键岗位职责，
         *    建立了风险管理制度和相应偏差、变更等管理制度，实行了质量受权人管理制度。
         *   （二）机构与人员
         */
//        String scsjEmpty = "    ";
//        String scsjEmpty = "&nbsp;&nbsp;&nbsp;&nbsp;";
        String scsjEmpty = "";

        int order = 0;
        for (SpcyFgXcjczj zj : zjmlList){

            /*if (order>0){
                jczj += returnCharFlag;
                jczj += scsjEmpty;
            }*/

            jczj += "<p>&nbsp;&nbsp;&nbsp;&nbsp;";
            jczj += "（";
            jczj += ChineseNumberConverterUtils.convertToChinese(zj.getForder());
            jczj += "）";
//            jczj += "、";

            if (StringUtils.isNotBlank(zj.getFzjmc())) {
                String[] split = zj.getFzjmc().split("、");
                if (split.length > 1) {
                    jczj += split[1];
                }else {
                    jczj += zj.getFzjmc();
                }
            }
            jczj += "</p>";

            if (StringUtils.isNotBlank(zj.getFjcms())){
                if (!zj.getFjcms().startsWith("<p")){
                    jczj += "<p>";
                    jczj += zj.getFjcms();
                    jczj += "</p>";
                }else {
                    jczj += zj.getFjcms();
                }
            }else {
                jczj += "<p>&nbsp;&nbsp;&nbsp;&nbsp;</p>";
            }

            order++;
        }

        resultMap.put("${data.jczj}", jczj);
        return resultMap;
    }

    /** 获取现场检查专述 */
    public List<Map<String, Object>> genXcjcJcqkzsContentObject(String sqsxid, String jcqkzs){
        if (StringUtils.isBlank(jcqkzs)){
            jcqkzs = spcySqsxBaiscService.selectJcqkzsById(sqsxid);
        }

        List<Map<String, Object>> jczsList = new ArrayList<>();
//        List<String> paragraphs = HtmlSplitUtils.splitParagraphs(jcqkzs);
        List<String> paragraphs = HtmlSplitUtils.extractParagraphsPreservingTables(jcqkzs);
        for (String jczs : paragraphs) {
            Map<String, Object> jczsMap = new HashMap<>();
            jczsMap.put("jcqkzs", jczs);
            jczsList.add(jczsMap);
        }
        return jczsList;
    }

    /** 获取现场检查章节描述 */
    public List<Map<String, Object>> genXcjczjMsContentObject(String jcqkzs){
        if (StringUtils.isBlank(jcqkzs)){
            return null;
        }
        List<Map<String, Object>> jczsList = new ArrayList<>();
//        List<String> paragraphs = HtmlSplitUtils.splitParagraphs(jcqkzs);
        List<String> paragraphs = HtmlSplitUtils.extractParagraphsPreservingTables(jcqkzs);
        for (String jczs : paragraphs) {
            Map<String, Object> jczsMap = new HashMap<>();
            jczsMap.put("zjms", jczs);
            jczsList.add(jczsMap);
        }
        return jczsList;
    }

    /** 获取现场检查章节情况汇总模板数据 */
    public List<Map<String, Object>> genXcjczjStructureContentObject(String sqsxid){
        List<Map<String, Object>> list = new ArrayList<>();

        // 获取对应模板的章节
        List<SpcyFgXcjczj> zjmlList = spcyFgXcjczjService.list(new LambdaQueryWrapper<SpcyFgXcjczj>()
                .eq(SpcyFgXcjczj::getFsqsxid, sqsxid)
                .orderByAsc(SpcyFgXcjczj::getForder));
        if (CollectionUtil.isEmpty(zjmlList)){
            return list;
        }

        /**
         *   （一）质量管理
         *    该公司建立了质量管理体系，制定了质量方针和质量目标，明确了各部门及关键岗位职责，
         *    建立了风险管理制度和相应偏差、变更等管理制度，实行了质量受权人管理制度。
         *   （二）机构与人员
         */
        // 发现问题
        List<SpcyFgXcjcwt> wtList = this.queryListBySqsxId(sqsxid);

        for (SpcyFgXcjczj zj : zjmlList){

            Object jczj = "";
            jczj += "<p>&nbsp;&nbsp;&nbsp;&nbsp;";
            /*jczj += "（";
            jczj += ChineseNumberConverterUtils.convertToChinese(zj.getForder());
            jczj += "）";*/

            jczj += zj.getFzjmc();

            /*if (StringUtils.isNotBlank(zj.getFzjmc())) {
                *//*String[] split = zj.getFzjmc().split("、");
                if (split.length > 1) {
                    jczj += split[1];
                }else {
                    jczj += zj.getFzjmc();
                }*//*
                jczj += StringPatternUtils.indexStringWithChar(zj.getFzjmc(), '）');
            }*/
            jczj += "</p>";

            /*if (StringUtils.isNotBlank(zj.getFjcms())){
                if (!zj.getFjcms().startsWith("<p")){
                    jczj += "<p>";
                    jczj += zj.getFjcms();
                    jczj += "</p>";
                }else {
                    jczj += zj.getFjcms();
                }
            }else {
                jczj += "<p>&nbsp;&nbsp;&nbsp;&nbsp;</p>";
            }*/

            Object fxwt = this.getJczjQxwt(sqsxid, zj.getId(), wtList);

            HashMap<String, Object> parMap3 = new HashMap<>();
            parMap3.put("jczj", jczj);
            parMap3.put("jcmss", this.genXcjczjMsContentObject(zj.getFjcms()));
            parMap3.put("fxwt", fxwt);

            list.add(parMap3);
        }

        return list;
    }

    /** 获取检查章节-发现问题 */
    private String getJczjQxwt(String sqsxid, String jczjid, List<SpcyFgXcjcwt> wtList){
        String fxwt = "";

        if (CollectionUtil.isEmpty(wtList)){
            fxwt += "检查发现问题：无";
            return "<p>&nbsp;&nbsp;&nbsp;&nbsp;" + fxwt + "</p>";
        }

        /*String returnCharFlag = "<p><br/>";
        */
        String scsjEmpty = "&nbsp;&nbsp;&nbsp;&nbsp;";

        List<SpcyFgXcjcwt> yzwtList = wtList.stream()
                .filter(f ->
                        StringUtils.isNotBlank(f.getFxcjczjid())
                                &&
                                f.getFxcjczjid().equals(jczjid)
                                && StringUtils.isNotBlank(f.getFyzcd())
                                && f.getFyzcd().equals(SpcyCommonConstant.FLFG_XCJCWT_YZCD_YZQX)
                )
                .collect(Collectors.toList());

        List<SpcyFgXcjcwt> zywtList = wtList.stream()
                .filter(f ->
                        StringUtils.isNotBlank(f.getFxcjczjid())
                                &&
                                f.getFxcjczjid().equals(jczjid)
                                && StringUtils.isNotBlank(f.getFyzcd())
                                && f.getFyzcd().equals(SpcyCommonConstant.FLFG_XCJCWT_YZCD_ZYQX)
                )
                .collect(Collectors.toList());

        List<SpcyFgXcjcwt> ybwtList = wtList.stream()
                .filter(f ->
                        StringUtils.isNotBlank(f.getFxcjczjid())
                                &&
                                f.getFxcjczjid().equals(jczjid)
                                && StringUtils.isNotBlank(f.getFyzcd())
                                && f.getFyzcd().equals(SpcyCommonConstant.FLFG_XCJCWT_YZCD_YBQX)
                )
                .collect(Collectors.toList());


        int order = 1;
        if (CollectionUtil.isNotEmpty(yzwtList)){

            if (order == 1){
                fxwt += scsjEmpty;
                fxwt += "检查发现问题：";
            }

            for (SpcyFgXcjcwt wt : yzwtList){
                fxwt += "<br>";
                fxwt += scsjEmpty;

                fxwt += order + "、";
                fxwt += (Optional.ofNullable(wt.getFwtms()).orElseGet(()->" "));
                fxwt += this.getWttkContent(wt.getId());
                order++;
            }
        }

        if (CollectionUtil.isNotEmpty(zywtList)){
            if (order == 1){
                fxwt += scsjEmpty;
                fxwt += "检查发现问题：";
            }

            for (SpcyFgXcjcwt wt : zywtList){
                fxwt += "<br>";
                fxwt += scsjEmpty;

                fxwt += order + "、";
                fxwt += (Optional.ofNullable(wt.getFwtms()).orElseGet(()->" "));
                fxwt += this.getWttkContent(wt.getId());
                order++;
            }
        }

        if (CollectionUtil.isNotEmpty(ybwtList)){
            if (order == 1){
                fxwt += scsjEmpty;
                fxwt += "检查发现问题：";
            }

            for (SpcyFgXcjcwt wt : ybwtList){
                fxwt += "<br>";
                fxwt += scsjEmpty;

                fxwt += order + "、";
                fxwt += (Optional.ofNullable(wt.getFwtms()).orElseGet(()->" "));
                fxwt += this.getWttkContent(wt.getId());
                order++;
            }
        }

        if (StringUtils.isBlank(fxwt)){
            fxwt += "<p>&nbsp;&nbsp;&nbsp;&nbsp;检查发现问题：无";
        }

        return "<p>" + fxwt + "</p>";
    }

    /** 问题条款 */
    private String getWttkContent(String fwjid){
        String tknr = "";
        List<SpcyFgXcjcwttk> tkList = this.queryListXcjcwttk(fwjid);
        for (int m = 0; m < tkList.size(); m++){
            if (m == 0){
                tknr += "（";
            }else {
                tknr += "、";
            }

            tknr += Optional.ofNullable(tkList.get(m).getFfgtk()).orElseGet(()->" ");

            if (m == (tkList.size()-1)) {
                tknr += "）";
            }
        }

        return tknr;
    }
}
